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
how to get value of clicked row column of gridview?
提问by Mogli
this is the code of gridview
in .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....
}
}
}