Ask a Question related to ASP.NET Data Grid Control, Design and Development.
Joel Finkel #1
Cells.Text or Cells.Controls
The following code illustrates two methods of obtaining the contents of a DataGrid Item. The function has been bound to the ItemCommand of the DataGridCommandEventHandler. It is invoked by clicking a button within the row.
Note that because I am loathe to hard-code column numbers, as they may change, I at least make the code a bit easier to maintain by setting constants to represent the column numbers I need to use.
In the first example, I get the Address by simply using the Text attribute of e.Item.Cells.
In the second example, which is the more common in the documentation, I use the two-step process of obtaining the object, e.Item.Cells.Columns as a TextBox, and then getting its Text attribute.
QUESTION: Why is the second example more common in the documentation when the first method seems to work, is easier to code, and is faster?
QUESTION: Am I missing something terribly important here?
private void DataGrid1_ItemCommand(object sender, DataGridCommandEventArgs e)
const int k_p_Address = 7;
const int k_p_City = 8;
const int k_p_State = 9;
const int k_p_Zip = 10;
const int k_p_Country = 11;
myAddressCorrector.Address = e.Item.Cells[k_p_Address].Text;
TextBox CityText = (TextBox)e.Item.Cells[k_p_City].Controls;
myAddressCorrector.City = CityText.Text;
TextBox StateText = (TextBox)e.Item.Cells[k_p_State].Controls;
myAddressCorrector.State = StateText.Text;
TextBox ZipText = (TextBox)e.Item.Cells[k_p_Zip].Controls;
myAddressCorrector.Zip = ZipText.Text;
TextBox CountryText = (TextBox)e.Item.Cells[k_p_Country].Controls;
myAddressCorrector.Country = CountryText.Text;
catch (Exception ex)
Label1.Text = ex.ToString();
Joel Finkel Guest
I am new at this.. probably not proficient enough to even consider myself a Newbie yet. :-) I have a DataGrid with Template columns. After the...
How can I retrieve the Container.DataItem name in a checkbox used in a template column. During an Update, I can dynamically retrieve the value of...
The code could not be simpler: Dim txtProjectID As TextBox = CType(e.Item.Cells(1).Controls(0), TextBox) Dim dblProjectID As Double =...
Dear All, Is it possible to use to the datagrid to take inputs from the user by placing controls in the cells of the grid and saving everything...
I have a grid with several fields an many records. For fields that have a large amount of text in them, I want to show a button that the user can...