2015-09-28 77 views
0
protected void select_click(object sender, GridViewCommandEventArgs e) 
{ 
    try 
    { 
     DBLibrary db = new DBLibrary(); 
     int index = Convert.ToInt32(e.CommandArgument); 
     * string FeeId = gridv1.Rows[index].Cells[1].Text;* 


     if (e.CommandName == "Select") 
     { 


      string str = "SELECT AnuFeeMaster.FeeId ,AnuFeeMaster.StudentId, Tbl_Student.SName, AnuFeeMaster.Month, AnuFeeMaster.Year, AnuFeeMaster.FeeAmount, " + 
        " AnuFeeMaster.PaidAmount FROM AnuFeeMaster INNER JOIN Tbl_Student ON AnuFeeMaster.StudentId = Tbl_Student.StudentId where (AnuFeeMaster.FeeId ='" + FeeId + "')"; 
      SqlDataReader dr = db.ExecuteReader(str); 

      while (dr.Read()) 
      { 

      Session["name"] = dr["sname"].ToString(); 
      Session["id"] = dr["StudentId"].ToString(); 
      Session["mth"] = dr["Month"].ToString(); 
      Session["yr"] = dr["Year"].ToString(); 
      Session["tot"] = dr["FeeAmount"].ToString(); 


      } 
     } 
    } 
    catch { } 
} 

上面是我的代碼,我用來訪問,我沒有得到從RAT值r數據請建議我,*標記我用哪個顯示我在哪裏得到錯誤使用行索引在gridview中選擇行

+0

_「我在哪裏得到錯誤」_什麼錯誤?你能顯示aspx嗎? –

+0

你得到的例外是什麼? – Canavar

+0

數據不是retriveing ..,從單元格的選定行的gridview。,null值得到@ Canavar – Vinay

回答

0

您應該正確使用gridview的事件RowCommand

void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e) 
{ 
if(e.CommandName=="Select") 
{ 
    int index = Convert.ToInt32(e.CommandArgument); 
    GridViewRow selectedRow = CustomersGridView.Rows[index]; 
} 
} 

您可以捕捉按鈕點擊事件象下面這樣:

protected void MyButtonClick(object sender, System.EventArgs e) 
{ 
//Get the button that raised the event 
Button btn = (Button)sender; 

//Get the row that contains this button 
GridViewRow gvr = (GridViewRow)btn.NamingContainer; 
} 
+0

我已經嘗試過該代碼,但它不工作 – Vinay

+0

您使用的是boundfield或templatefield? – 2015-09-28 10:54:18

+0

我已經使用模板字段 – Vinay

0

創造適當的電網rowcommand事件 ASPX代碼

<asp:GridView ID="Gv" runat="server" AllowPaging="true" OnRowCommand="Gv_RowCommand" PageSize="10" EmptyDataText="No Records Found !"> 
    <Columns> 
     <asp:TemplateField HeaderText="Action" ItemStyle-Width="20%"> 
      <ItemTemplate> 
       <asp:LinkButton ID="lnkview" runat="server" CommandArgument='<%#Eval("Demo_Code") %>' CommandName="select" ></asp:LinkButton> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:BoundField DataField="Coulmn1" HeaderText="Coulmn2" /> 
     <asp:BoundField DataField="Coulmn2" HeaderText="Coulmn2" /> 
    </Columns> 
</asp:GridView> 

aspx.cs代碼

protected void Gv_RowCommand(object sender, GridViewCommandEventArgs e) 
{ 
     if (e.CommandName == "select") 
     { 

     } 
}