2015-09-21 85 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;假設標籤是用來顯示條形碼等等