gridview
how to add rows dynamically to a gridview????
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Lalit MPosted Jan 27, 2010, 5:43 AM
Private orderTotal As Decimal = 0.0D
Private tax As Decimal = 0.0D
Protected Sub gvOrderDetail_RowDataBound(sender As Object, e As GridViewRowEventArgs)
'TODO: Calculate the orderTotal and the tax when RowType = DataRow
If e.Row.RowType = DataControlRowType.Footer Then
'Create the Row
Dim row As New GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal)
'Add the two Columns
row.Cells.AddRange(CreateCells())
'get a reference to the table that holds this row
Dim tbl As Table = (TryCast(e.Row.Parent, Table))
'Add the row at the end of the list, but before the footer.
tbl.Rows.AddAt(gvOrderDetail.Rows.Count + 1, row)
'Don't forget to account for any changes in the footer. Since we added a row to show the tax,
'that tax must also be accounted for in our footer. Calculating the orderTotal and the tax
'is an exercise for the reader.
Dim lbl As Label
lbl = DirectCast(e.Row.FindControl("lblTotal"), Label)
lbl.Text = [String].Format("{0:C}", (orderTotal + tax))
End If
End Sub
Private Function CreateCells() As TableCell()
Dim cells As TableCell() = New TableCell(2) {}
Dim cell As TableCell
Dim lbl As Label
'The order item column
cell = New TableCell()
lbl = New Label()
lbl.Text = "Sales Tax"
cell.Controls.Add(lbl)
cells(0) = cell
'The price column
cell = New TableCell()
lbl = New Label()
lbl.Font.Bold = True
lbl.Text = tax.ToString("C")
cell.HorizontalAlign = HorizontalAlign.Right
cell.Controls.Add(lbl)
cells(1) = cell
Return cells
End Function
more sample in C#
Check here
Lalit MPosted Jan 27, 2010, 5:37 AM
decimal orderTotal = 0.0m;
decimal tax = 0.0m;
protected void gvOrderDetail_RowDataBound(object sender, GridViewRowEventArgs e)
{
//TODO: Calculate the orderTotal and the tax when RowType = DataRow
if (e.Row.RowType == DataControlRowType.Footer)
{
//Create the Row
GridViewRow row = new GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal);
//Add the two Columns
row.Cells.AddRange(CreateCells());
//get a reference to the table that holds this row
Table tbl = (e.Row.Parent as Table);
//Add the row at the end of the list, but before the footer.
tbl.Rows.AddAt(gvOrderDetail.Rows.Count + 1, row);
//Don't forget to account for any changes in the footer. Since we added a row to show the tax,
//that tax must also be accounted for in our footer. Calculating the orderTotal and the tax
//is an exercise for the reader.
Label lbl;
lbl = (Label)e.Row.FindControl("lblTotal");
lbl.Text = String.Format("{0:C}", (orderTotal + tax));
}
}
private TableCell[] CreateCells()
{
TableCell[] cells = new TableCell[2];
TableCell cell;
Label lbl;
//The order item column
cell = new TableCell();
lbl = new Label();
lbl.Text = "Sales Tax";
cell.Controls.Add(lbl);
cells[0] = cell;
//The price column
cell = new TableCell();
lbl = new Label();
lbl.Font.Bold = true;
lbl.Text = tax.ToString("C");
cell.HorizontalAlign = HorizontalAlign.Right;
cell.Controls.Add(lbl);
cells[1] = cell;
return cells;
}
for reference
more info
http://www.c-sharpcorner.com/UploadFile/Art%20Scott/GridViewInsertANewRow09012006165053PM/GridViewInsertANewRow.aspx
Adding Dynamic Rows in GridView with TextBoxes