Gridview选中的行返回null
本文关键字:返回 null Gridview | 更新日期: 2023-09-27 18:13:53
我在我的aspx文件中有gridview包含链接按钮:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="Button3" Style="float: left; margin-left: 10px"
CommandArgument='<%# Bind("ID") %>'
Text="cancellation" runat="server" CommandName="cancell" OnClick="Button3_Click">
<i aria-hidden="true" class="icon-lock" title="cancellation"></i>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
我想当用户点击链接按钮更新我的数据库表,但当我想从gridview选定行中获取单元格值时,我面对空引用异常:字符串条形码= dgvData.SelectedRow.Cells[12]. text;.
protected void Button3_Click(object sender, EventArgs e)
{
Transaction tr = new Transaction();
HasinReservation.Entities.Db.Transaction dt = new Transaction();
SqlConnection connection = new SqlConnection(@"Data Source=192.x.x.x'Sql2008;Initial Catalog=GardeshgariKish;User ID=cms;Password=******;MultipleActiveResultSets=True;Application Name=EntityFramework");
connection.Open();
SqlCommand sqlCmd = new SqlCommand("Update Transactions SET IsCancelled = 1 WHERE BarCodeNumber = @Value", connection);
string barcode = dgvData.SelectedRow.Cells[12].Text;
sqlCmd.Parameters.AddWithValue("@Value", barcode);
//sqlCmd.Parameters.AddWithValue("@Value2", DropDownList2.SelectedItem.Text);
sqlCmd.ExecuteNonQuery();
connection.Close();
}
dgvData.SelectedRow
不会给出单击按钮的行。你需要NamingContainer
。这应该对你有用:-
LinkButton Button3 = (LinkButton)sender;
GridViewRow selectedRow = (GridViewRow)Button3.NamingContainer;
string barcode = selectedRow.Cells[12].Text;
您可以尝试用这种方式从GridView获取值。我写的不是你的全部代码,而是包含错误的部分。
protected void Button3_Click(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewRow row = this.Rows[e.RowIndex];
string barcode = row.Cells["ID"].Value.ToString();
}
}
但是我可以从你的代码中看到,你正在使用GridView的编辑模式,但没有利用它到你的代码。在Asp.Net中编辑和更新GridView中的行
GridView.SelectedRow
属性将仅在您选择一行时填充该行。最简单的方法是将属性 autogenerateselectbutton
设置为true
:
<asp:gridview id="CustomersGridView" autogenerateselectbutton="True" .../>
您必须依次执行以下步骤:
- 通过点击选择按钮选择一行
- 然后点击链接按钮。在Button Click事件中,现在您将像往常一样拥有选定的行。
另外,dgvData.SelectedRow.Cells[12].Text
意味着单元格/列12应该是BoundField
。
如果第12列是模板字段,使用FindControl()
方法作为dgvData.SelectedRow.FindControl("lblBarCode") as Label).Text;
假设一个标签被用来显示条形码等…
这是因为您没有使用选择命令
应该这样做:
-
添加RowCommand事件到GridView
onrowcommand="GridView1_RowCommand"
-
将命令参数
id
替换为BarCode并删除OnClick="Button3_Click"
<asp:LinkButton ID="Button3" CommandArgument='<%# Bind("BarCode")
-
接收值
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "cancell") { string barcode = e.CommandArgument; } }
谢谢你的关心我找到了这是我编辑的asp标记:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="Button3" Style="float: left; margin-left: 10px"
CommandArgument='<%# Bind("ID") %>'
Text="cancellation" runat="server" **CommandName="select"** OnClick="Button3_Click">
<i aria-hidden="true" class="icon-lock" title="cancellation"></i>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
后面的代码是:
protected void Button3_Click(object sender, EventArgs e)
{
Transaction tr = new Transaction();
**GridViewRow clickedRow = ((LinkButton)sender).NamingContainer as GridViewRow;**
HasinReservation.Entities.Db.Transaction dt = new Transaction();
**int x = clickedRow.RowIndex;**
SqlConnection connection = new SqlConnection(@"Data Source=192.X.X.X'Sql2008;Initial Catalog=GardeshgariKish;User ID=cms;Password=XXXXXX;MultipleActiveResultSets=True;Application Name=EntityFramework");
connection.Open();
SqlCommand sqlCmd = new SqlCommand("Update Transactions SET IsCancelled = 1 WHERE BarCodeNumber = @Value", connection);
**string barcode = dgvData.Rows[x].Cells[12].Text;**
sqlCmd.Parameters.AddWithValue("@Value", barcode);
sqlCmd.ExecuteNonQuery();
connection.Close();
}