如何获取页面上网格视图的选定行单元格值?

问题描述

| 我在页面上使用了网格视图。 我想在页面按钮的单击事件上通过ѭ0show显示所选行单元格的数据。     

解决方法

注意:: 请设置您的
CommandName
to2ѭ 请为 第二个按钮,您将使用 删除 至
\"deleteCol\"
设置按钮的
command argument
属性: .aspx
CommandArgument=\'<%#((GridViewRow)Container).RowIndex%>\'
CommandArgument=\'<%#((GridViewRow)Container).RowIndex%>\'
两个按钮。 .cs
 protected void gv_RowCommand(object sender,GridViewCommandEventArgs e)
    {
        try
        {
            int index = Convert.ToInt32(e.CommandArgument);
            if (e.CommandName == \"selectCol\")
            {
                Response.Write(gv.Rows[index].Cells[0].Text); //consider you use bound field and the column you want to show its value is the first column.
            }

            else if(e.CommandName == \"deleteCol\")
             {
                 int id = int.Parse(gv.DataKeys[index].Value.ToString());//the primary key for your table.
                 Delete(id);//method which use (Delete From .... Where id = ....).
             }


            gv.DataBind();

        }

        catch (Exception ee)
        {
            string message = ee.Message;
        }
    }
    ,问候他。 从gridview字段读取值的最简单方法是编写: your_grid_name.SelectedRow.Cell(* number_of_index *)。text 以我为例: 昏暗的owner_name作为字符串 雇主名称= poslodavac_grid.SelectedRow.Cells(1)。文本 只需记住,第一个单元格索引为零,并且不将\“ asp:CommandField ShowSelectButton \”标记为第一个。     ,使用GridView.SelectedRow属性。
String cellText = this.gridView.SelectedRow.Cells[cellIndex].Text;
有关在
GridView
控件中选择行的信息,请参阅以下内容。 在ASP.Net中的GridView控件中选择“命令”     ,如果在网格视图中使用
LINK BUTTON
,则可以在
ROWCOMMAND
方法中使用以下代码。该代码用于检索特定选定行中的所有值。
 // to get the value of the link use the command argument 
 FaultId = Convert.ToInt32(e.CommandArgument);

 // to get the other column values 

UserId = Convert.ToInt32(((GridViewRow(((LinkButton)e.CommandSource).NamingContainer)).Cells[1].Text);  

Department = ((GridViewRow(((LinkButton)e.CommandSource).NamingContainer)).Cells[2].Text;

ProblemType = ((GridViewRow)(((LinkButton)e.CommandSource).NamingContainer)).Cells[3].Text;
    ,您可以在gridview的RowCommand事件中获取它:
protected void GridView1_RowCommand(object sender,GridViewCommandEventArgs e)

{
    if (e.CommandName == \"Select\")
    {
        GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
        Response.Write(row.Cells[0].Text);
        Response.Write(row.Cells[1].Text);
        ................
    }
}