我的gridview在我的aspx文件中包含linkbutton,如下面的cod:
<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选择的行中获取单元格值时,我将面对null refrence exception行:string barcode = 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();
}
答案 0 :(得分:2)
dgvData.SelectedRow
无法为您提供单击按钮的行。你需要NamingContainer
。这应该适合你: -
LinkButton Button3 = (LinkButton)sender;
GridViewRow selectedRow = (GridViewRow)Button3.NamingContainer;
string barcode = selectedRow.Cells[12].Text;
答案 1 :(得分:2)
因为您没有使用select-command
要走的路应该是:
将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;
}
}
答案 2 :(得分:2)
您可以尝试这种方式从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的编辑模式但没有将它用于你的代码中。请参阅此link以在Asp.Net中的GridView中编辑和更新行。
答案 3 :(得分:1)
感谢您对我的关注。 这是我编辑的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();
}
答案 4 :(得分:1)
GridView.SelectedRow
属性才会填充该行。最简单的方法是将属性autogenerateselectbutton
设置为true
:
<asp:gridview id="CustomersGridView" autogenerateselectbutton="True" .../>
您必须按顺序执行以下步骤:
此外,dgvData.SelectedRow.Cells[12].Text
表示单元格/列12应为BoundField
。
如果第12列是模板字段,请使用FindControl()
方法作为dgvData.SelectedRow.FindControl("lblBarCode") as Label).Text;
,假设使用标签显示条形码等...