I want to access a certain cell in a row of a datagrid by name instead of index just in case the columns get reordered(Which has happened already)
MyBox.Text = ListAllCategories.Rows[ListAllCategories.SelectedIndex].Cells[1].Text;
Instead of using "Cells[1]", I want to do something like "Cells["Name"]"(Which didn't work) or something sim开发者_JAVA百科ilar where "Name" is the name of the column inside the GridView.
Can anyone tell me how to do this?
Don't use indexes they fail when more columns are created.
Instead use MyGrid.FindControl("NameOfYourControl").
For example assume there is a label called lblID, a grid view called gvMyGridView.
Try this:
Dim l as Label
l = Me.gvMyGridView.FindControl("lblID")
You can also use CType to cast it or get the text contents of this label, etc.
精彩评论