我想通过按钮点击从grideview获取单元格值 [英] I want to get cell value from grideview on button click

查看:112
本文介绍了我想通过按钮点击从grideview获取单元格值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有网格视图,我想从一列获取单元格值并使用该值导航到另一个页面,显示该学生的完整个人资料

而不是从单元格中获取值仅零作为参数



我尝试过:



i have grid view and i want to take cell value from one column and use that value to navigate to another page that shows complete profile of that student
instead of taking value from cell it is only taking zero as argument

What I have tried:

protected void Button1_Click(object sender, EventArgs e)
    {
        string year = TextBox1.Text;
        studentDataContext db = new studentDataContext();
        var stud = from p in db.s_profs
                   where p.stdpyear == TextBox1.Text || p.stdname == TextBox2.Text
                   select new { p.stud_id, p.stitle, p.stdname, p.stdcurcity, p.stdorgnm, p.stdpyear, p.stdcourse };

        /* var q =
             from a in db.GetTable<stud_reg>()
             where a.uyear == TextBox1.Text
             select  a.category, a.uprefix,a a.uemail ;*/
        GridView1.DataSource = stud;
        GridView1.DataBind();
    }







protected void btnSubmit_Click(object sender, EventArgs e)
   {
       Response.Redirect("gridprof.aspx?stud_id =" + studu);
   }










protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    GridViewRow row = GridView1.SelectedRow;
    studu = Convert.ToInt16(row.Cells[1].Text);

    Response.Write(studu);

}

推荐答案

您的问题可能没有多大原因,例如 row.Cells [1] 不包含学生ID或传递给的值。转换.ToInt16()方法超过 Int16.MaxValue [ ^ ]。br />


我会尝试以这种方式获得学生ID:

There might be few reasons of your issue, for example row.Cells[1] does not contain student id or a value passed to the Convert.ToInt16() method exceeds Int16.MaxValue[^].

I'd try to get student id this way:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
      // Convert the row index stored in the CommandArgument
      // property to an Integer.
      int index = Convert.ToInt32(e.CommandArgument);
      // Retrieve the row that contains the button clicked 
      // by the user from the Rows collection.
      GridViewRow row = ContactsGridView.Rows[index];
     studu = Convert.ToInt32(row.Cells[1].Text);
}





更多来自MSDN:GridView.RowCommand Event(System.Web.UI.WebControls) [ ^ ]


这篇关于我想通过按钮点击从grideview获取单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆