Tuesday, May 25, 2004

Better DataGrid insert method

After spending way too much time yesterday figuring out how to get a DataGrid footer's contents, this morning I found something that is much easier in the right situation.

For some reason Adding a New Record to a DataGrid from 4GuysFromRolla made its way into my Google search. Their solution is to use an Add button in the footer row, hook into the DataGrid's ItemCommand event, and redirect to an "insert into...." method when the command is the Add button.

In my current case, the Add button needs to be outside the DataGrid, so I can't try this solution now. However, I will be very interested to use it next time because it is a much, much easier mechanism for collecting the user's input in the footer controls. The ItemCommand event includes a DataGridCommandEventArgs parameter which provides easy access to the controls. For example:

   TextBox nameCtrl = e.Item.FindControl("nameTextBox")).
   string name = nameCtrl.Text;

No comments: