Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从DataGridView获取选定行的单元格内容_C#_Asp.net_Gridview - Fatal编程技术网

C# 从DataGridView获取选定行的单元格内容

C# 从DataGridView获取选定行的单元格内容,c#,asp.net,gridview,C#,Asp.net,Gridview,这是检索所选行中单元格内容的正确方法吗?如果我使用这个,我就得不到价值。所以,请建议最好的方法。是的,这是一种方法,你也可以这样做: string FeeId = grdvw.Rows[index].Cells[0].Text; 此外,如果要从所有行中获取值,可以像下面这样迭代Datagridview: string FeeId = grdvw.Rows[index].Cells[0].Value; 当我尝试时,我得到了价值。下面是我在运行时添加行和列的示例: foreach (DataGr

这是检索所选行中单元格内容的正确方法吗?如果我使用这个,我就得不到价值。所以,请建议最好的方法。

是的,这是一种方法,你也可以这样做:

string FeeId = grdvw.Rows[index].Cells[0].Text;
此外,如果要从所有行中获取值,可以像下面这样迭代Datagridview:

string FeeId = grdvw.Rows[index].Cells[0].Value;

当我尝试时,我得到了价值。下面是我在运行时添加行和列的示例:

foreach (DataGridViewRow row in grdvw.Rows)
{
   string column0 = row.Cells[0].Value;
   string column1 = row.Cells[1].Value;
   //More code here
}
public分部类WebForm1:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
DataTable dt=新的DataTable();
DataColumn dCol1=新的DataColumn(“Col1”,typeof(System.String));
DataColumn dCol2=新的DataColumn(“Col2”,typeof(System.String));
dt.Columns.Add(dCol1);
dt.Columns.Add(dCol2);
对于(int i=0;i<2;i++)
{
DataRow row1=dt.NewRow();
行1[“Col1”]=“一”;
第1行[“Col2”]=“2”;
dt.行。添加(第1行);
}
foreach(dt.列中的数据列列列)
{
BoundField bField=新的BoundField();
bField.DataField=col.ColumnName;
bField.HeaderText=列名称;
GridView1.Columns.Add(bField);
}
GridView1.DataSource=dt;
//将datatable与GridView绑定。
GridView1.DataBind();
字符串str=GridView1。行[0]。单元格[0]。文本;
}
}

如果我们这样使用,我们会得到答案

您使用的是or吗?使用Website@Vinay:请发布完整的代码,以便可以复制问题。先生,我正在使用网站。如果我使用“.value”,因为它显示错误,请发布一些对其他人有用的内容。
public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        DataColumn dCol1 = new DataColumn("Col1", typeof(System.String));
        DataColumn dCol2 = new DataColumn("Col2", typeof(System.String));
        dt.Columns.Add(dCol1);
        dt.Columns.Add(dCol2);
        for (int i = 0; i < 2; i++)
        {
            DataRow row1 = dt.NewRow();
            row1["Col1"] = "One";
            row1["Col2"] = "Two";
            dt.Rows.Add(row1);
        }
        foreach (DataColumn col in dt.Columns)
        {
            BoundField bField = new BoundField();
            bField.DataField = col.ColumnName;
            bField.HeaderText = col.ColumnName;
            GridView1.Columns.Add(bField);
        }
        GridView1.DataSource = dt;
        //Bind the datatable with the GridView.
        GridView1.DataBind();
        string str = GridView1.Rows[0].Cells[0].Text;
    }
}
string FeeId = (grdvw.Rows[index].Cells[0].FindControl("lblfee") as Label).Text;