开发者

ASP.NET GridView Access Cell By Name

开发者 https://www.devze.com 2022-12-08 09:31 出处:网络
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)

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.
0

精彩评论

暂无评论...
验证码 换一张
取 消