Datagrid inserting rows question.
I am always looking for better solutions for a quick and clean code for table maintenance.
More for fun than anything I am writing a table maintenance page using datagrid. I am trying to keep it as abstract as possible, as I will later add other tables and reuse most code, or maybe wrap it in a custom control.
The datagrid is sorting asc and desc in all columns ( displaying an arrow showing the direction), allowing the user to pick how many rows to be displayed at a time, editing and deleting.
I want to make it insert too, and for that I have a button outside the datagrid, “Add Row“, that when clicked adds an empty row to the top of the dataset, saves the dataset, set currentpageindex and edititemindex and binds.
Private Sub btnAddRow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddRow.Click
SqlDataAdapter_store.Fill(DataSet_store)
Dim dr As DataRow = Me.DataSet_store.atr_store.NewRow
dr("store") = ""
dr("size") = ""
dr("active") = True
Me.DataSet_store.atr_store.Rows.InsertAt(dr, 0)
Session("DataSet_store") = DataSet_store
dgStores.CurrentPageIndex = 0
dgStores.EditItemIndex = 0
viewstate("Operation") = "INSERT"
Binddata(True)
End Sub
The problem I am finding is with the primary key column.
Before get to the insert part, I had it as a bound column, only showing the data (“store“). But using this approach to insert rows I need to have a textbox in the edititemteplate tag in that column, and the column needs to be a template column instead of databound column. All obvious, no problem.
But now when editing the “store“ textbox is available too.
I cannot make it readonly or disabled or invisible because either it hasn't been rendered yet and it doesn't exist, or it has been rendered and then it is too late to change it's attributes.
Also, I am trying to avoid adding client script to change the box attribute to read only after the page loads. I don't think it is elegant, and will probably byte me back..
I hate asking obvious questions about simple scenarios, but someone have a word on this?
Thanks
-Mauricio