The "normal" use of a web DataGrid with regards to data editing and update assumes the following sequence: the user selects the row which is to be edited, the row is transformed for editing (e.g., labels become textboxes etc.), values are changed, the Update button is pressed, data is written to the original data source (e.g., database), the grid is rebound, and the new values for the given row appear.
However, we might want to update a number of rows and commit all changes at once. In addition, the user may want to have both the original and the edited values stored in the same grid while having the edited ones appear only on item edit command.
This piece of code also solves the problem of copying columns within the same table or between various tables.
Using the code
This code is for those who have spent some time with the ASP
DataGrid - you will understand where to put the code. One of the important things is not to forget to properly configure the
DataGrid with regards to its column items, controls, bindings etc. Also, while the code presented below is quite flexible, it is still limited; therefore, we have to make some changes in the design. However, if you are willing to implement the ideas presented here and build a fully flexible MegaGrid control, welcome.
<asp:Label id=label1 runat="server"
text='<%# DataBinder.Eval(Container.DataItem, "value_A") %>'>
<asp:TextBox id=edit1 runat="server"
text='<%# DataBinder.Eval(Container.DataItem, "value_A_2") %>'>
<asp:TemplateColumn HeaderText="Rem Qty/Cr">
<asp:Label id=Label2 runat="server"
text='<%# DataBinder.Eval(Container.DataItem, "value_B") %>'>
<asp:TextBox id=e1 runat="server"
text='<%# DataBinder.Eval(Container.DataItem, "value_B_2") %>'>
Here is the code-behind:
Public Sub BindGrid(ByVal dt As DataTable)
dt = DataTableX2(dt, "_2")
dgrDetails.DataSource = dt
Function DataTableX2(ByVal name As DataTable, _
ByVal new_column_suffix As String) As DataTable
Dim c As Integer
Dim i As Integer
c = name.Columns.Count
For i = 0 To c - 1
Dim new_column As New DataColumn
new_column.ColumnName = _
name.Columns(i).ColumnName & new_column_suffix
Dim r As DataRow
For Each r In name.Rows
For i = 0 To c - 1
r.Item(i + c) = r.Item(i)
Private Sub dgrDetails_UpdateCommand(ByVal source As Object, _
ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) _
Me.dgrDetails.DataSource = UpdatedTable(e, _
CType(Session("Data_Source1"), DataTable), 3)
Me.dgrDetails.EditItemIndex = -1
Function UpdatedTable(ByVal e As _
ByVal Data_Source As DataTable, _
ByVal LeftOffset As Integer) As DataTable
Dim i As Integer
For i = 0 To dgrDetails.Columns.Count - 1 - LeftOffset
Data_Source.Rows(e.Item.ItemIndex).Item(i + CInt(Data_Source.Columns.Count / 2)) = _
CType(dgrDetails.Items(e.Item.ItemIndex).Cells(i + _
LeftOffset).FindControl("edit" + _
(i + 1).ToString), Web.UI.WebControls.TextBox).Text
Points of interest
Duplicating datatable columns as well as implementing common naming for bound datagrid controls are two things which will help me a lot in the future. Also, the whole idea of having virtually two datasources for the same datagrid is quite interesting.
"And though I have the gift of prophecy, and understand all mysteries, and all knowledge; and though I have all faith, so that I could remove mountains, and have not charity, I am nothing. And though I bestow all my goods to feed the poor, and though I give my body to be burned, and have not charity, it profiteth me nothing. Charity suffereth long, and is kind; charity envieth not; charity vaunteth not itself, is not puffed up, Doth not behave itself unseemly, seeketh not her own, is not easily provoked, thinketh no evil; Rejoiceth not in iniquity, but rejoiceth in the truth; Beareth all things, believeth all things, hopeth all things, endureth all things."