C# 如何获取gridview的点击行列的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15243833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:36:54  来源:igfitidea点击:

how to get value of clicked row column of gridview?

c#asp.netgridview

提问by Mogli

this is the code of gridviewin .aspx

这是gridview.aspx 中的代码

<asp:GridView ID="GridDaysPart" runat="server" CellPadding="4" ForeColor="#333333" 
        GridLines="None" Font-Names="Verdana" Font-Size="Smaller" 
        onrowcommand="GridDaysPart_RowCommand">
</asp:GridView>

and I am creating column in .cs file like this

我正在像这样在 .cs 文件中创建列

DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Sun", Type.GetType("System.Int32")));
dt.Columns.Add(new DataColumn("Mon", Type.GetType("System.Int32")));
dt.Columns.Add(new DataColumn("Tue", Type.GetType("System.Int32")));
dt.Columns.Add(new DataColumn("Wed", Type.GetType("System.Int32")));
dt.Columns.Add(new DataColumn("Thu", Type.GetType("System.Int32")));
dt.Columns.Add(new DataColumn("Fri", Type.GetType("System.Int32")));
dt.Columns.Add(new DataColumn("Sat", Type.GetType("System.Int32")));
return dt;

and then binding the gridview. now I want to get the value of the particular [row][cell], on which I click, I can get the row index using rowcommand event, but how to get to know that which column is clicked ? thanks in advance.

然后绑定gridview。现在我想获取我单击的特定 [row][cell] 的值,我可以使用 rowcommand 事件获取行索引,但是如何知道单击了哪一列?提前致谢。

回答by Mogli

You can hook on selectedindexchangedand get value by GridView.SelectedRow.Cells[0].Text

您可以挂钩selectedindexchanged并通过以下方式获取价值GridView.SelectedRow.Cells[0].Text

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    var value= String.Format("You selected row {0} with {1} {2}",
                                            GridView1.SelectedIndex + 1,
                                            GridView1.SelectedRow.Cells[0].Text,
                                            GridView1.SelectedRow.Cells[1].Text);
}

or use

或使用

var value= CustomersGridView.Rows[e.NewEditIndex].Cells[0].Text;

Edit: use this

编辑:使用这个

protected void GridView1_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
    int rownum = Convert.ToInt32(e.CommandArgument.ToString());
    foreach(GridViewRow row in sender.Rows)
    {
        if(row.Cells[0].Text == "a-value-")
        {
             // Do something....
        }
    }
}

回答by Mogli

try to GridView.RowEditing:

尝试GridView.RowEditing

String xxx = CustomersGridView.Rows[e.NewEditIndex].Cells[0].Text;

review this link

查看此链接

回答by Santosh

You can use DataGridView.CurrentCell property

您可以使用 DataGridView.CurrentCell 属性

int columnIndex = dataGridView.CurrentCell.ColumnIndex;
int rowIndex = dataGridView.CurrentCell.RowIndex;

You can refer this link

你可以参考这个链接