Wednesday, December 28, 2011

custom gridview in asp.net

Here i will show you how to create custom Data grid view and calculating total per entire column.For this i will go for create a data grid view control for accepting Products info and storing products write into Text file
code behind:
Imports System.IO
//form load event
DataGridView.RowCount=3
DataGridView.ColumnCount=3
DataGridView.Columns(0).HeaderText="desc"
DataGridView.Columns(1).HeaderText="Price"
DataGridView.Columns(2).HeaderText="Quantity"
DataGridView.Columns(3).HeaderText="Total"
End Sub
//calculating total Based on price,quantity
This requires "Cell Enter" event of DataGridView control,This event will provide current row index and column index
//This will executes when focus comes to cell
Private Sub dataGridView1_CellEnter(ByVal sender As Object, _
    ByVal e As DataGridViewCellEventArgs) _
    Handles dataGridView1.CellEnter

msgbox("row;"&e.RowIndex&&"col:"e.ColumnIndex)

if(e.ColumnIndex=3)Then
 With DataGridView1.Rows(e.RowIndex).cells(3).Value=Val(cells(2).Value)*val(cells(1).Value )
End With
total=quantity*price
with and end with will avoid repetition of DataGridiew.rows(e.RowIndex)

//logic for store into text file
private sub Button1_click(--)

Dim s As string
For i As Byte=0 to DataGridView1.Rows.Count-2
With DataGridView1.Rows(i)
s=s&DataGridView1.cells[0].Value&""&DataGridView1.cells[1].Value&""&DataGridView1.cells[2].Value&""&DataGridView1.cells[3].Value&
End With
Next

Dim sw As StreamWriter
sw=File.CreateText("Transinfo.txt)
sw.writeline("transeinfo "&vbCrLf&" "&"&vbCrLf&"&5
vbCrLf is visual basic carriage to return line feed[new line]
sw.Close()

End Sub

Change Grid view width Dynamically:
if (e.Row.RowType == DataControlRowType.Header) {
 for (i = 0; i <= e.Row.Cells.Count - 1; i++) {
  TableCell cell5 = e.Row.Cells(i);
  cell5.Width = new Unit("175px");
 }
}

Post a Comment

Bel