2015-09-21 88 views
1

我有GridView控件中包含的LinkBut​​ton如下面我aspx文件鳕鱼:GridView的选择行返回null

<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细胞的价值选定的行我面临空行参考异常行: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

因此,这行抛出异常,和你做了调查,为什么? –

+0

in this line: string barcode = dgvData.SelectedRow.Cells [12] .Text; –

+0

no不是这种情况,我gridview的选定行属性返回null。 –

回答

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(); 

     } 
2

dgvData.SelectedRow不会给你点击按钮的行。你需要NamingContainer。这应该为你工作: -

LinkButton Button3 = (LinkButton)sender; 
GridViewRow selectedRow = (GridViewRow)Button3.NamingContainer; 
string barcode = selectedRow.Cells[12].Text; 
2

那是因为你没有使用选择命令

要走的路应该是:

  • 添加RowCommand事件到你的GridView

    onrowcommand="GridView1_RowCommand" 
    
  • 替换命令参数id条码的d拔下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

你可以试试这个方式来获得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中的行。

1

GridView.SelectedRow属性将仅当您选择行的行进行填充。最简单的办法是将物业autogenerateselectbutton设置为true为:

<asp:gridview id="CustomersGridView" autogenerateselectbutton="True" .../> 

您必须执行以下步骤顺序:

  1. 通过单击选择按钮
  2. 之后选择一行点击链接按钮。在按钮点击事件现在你会像往常一样选择你的行。

另外,dgvData.SelectedRow.Cells[12].Text表示单元/列12应该是BoundField

如果列12模板字段,用FindControl()方法dgvData.SelectedRow.FindControl("lblBarCode") as Label).Text;假设标签是用来显示条形码等等