简体   繁体   English

网格视图选定的行单元格值变为空

[英]Grid View Selected row cell value getting null

I have a grid in my asp.net page . 我的asp.net页面上有一个网格。 Inside the selected index change event I implemented the below code . 在选定的索引更改事件中,我实现了以下代码。

 int ID = Convert.ToInt32(grdMnaualEntryTransactionTemplate.Rows[selectedIndex +1].Cells[1].Text); 

Cell1 is a number, in the Running time it gives me an error "Input string was not in a correct format" .It occurs due to "Text" is getting null in the running time. Cell1是一个数字,在运行时它给我一个错误“输入字符串的格式不正确”。它的发生是由于“文本”在运行时变为空。 I implemted below code too in the Selected index changed event it always gets a value. 我在选定的索引更改事件中也暗示了以下代码,它始终会得到一个值。

 int selectedIndex = grdMnaualEntryTransactionTemplate.SelectedIndex;

Why are you using selectedIndex + 1 ? 为什么要使用selectedIndex + 1 This will give you the row below the selected row, and will throw an exception when the user selects the last row in the grid. 这将为您提供所选行下方的行,并且当用户选择网格中的最后一行时将引发异常。

Try int ID = Convert.ToInt32(grdMnaualEntryTransactionTemplate.Rows[selectedIndex].Cells[1].Text); 尝试int ID = Convert.ToInt32(grdMnaualEntryTransactionTemplate.Rows[selectedIndex].Cells[1].Text); instead. 代替。

您可以这样使用..这是在gridview中获取选定行的数据的简单方法。请尝试以下代码。

int ID = Convert.ToInt32(grdMnaualEntryTransactionTemplate.SelectedRow.Cells[1].Text);

如果要选择值,则可以通过DataKeyName设置并调用SelectedIndexChanged事件。

string data = grdMnaualEntryTransactionTemplate.SelectedValue.ToString();

If you have the action buttons on the Grid, ie. 如果您在网格上有操作按钮,即 Select, Edit etc, then these will be in the Rows.Cells[] array. 选择,编辑等,然后它们将在Rows.Cells[]数组中。 Therefore if you have the three "Select" "Edit" "Delete" buttons per row, your first actual data cell will be array index[3]. 因此,如果每行具有三个“选择”,“编辑”,“删除”按钮,则第一个实际数据单元将为数组索引[3]。

As others have pointed out index+1 is incorrect as well. 正如其他人指出的那样,索引+1也是不正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM