C# 在C中获取datagridview中的数据

C# 在C中获取datagridview中的数据,c#,.net,winforms,C#,.net,Winforms,最新答复: 首先,您应该明确地将对数据库的调用与代码的其余部分分开。让我们创建一个GetData方法,它负责。。。获取数据: dataGridView1.Rows.Add(new DataGridViewRow()); 然后,要绑定DataGridView,您有两个选项: 使用数据绑定方法 以编程方式创建网格列和行 方法1-数据绑定方式: private DataSet GetData(string constr) { //'using' constructs are always a

最新答复:

首先,您应该明确地将对数据库的调用与代码的其余部分分开。让我们创建一个GetData方法,它负责。。。获取数据:

dataGridView1.Rows.Add(new DataGridViewRow());
然后,要绑定DataGridView,您有两个选项:

使用数据绑定方法 以编程方式创建网格列和行 方法1-数据绑定方式:

private DataSet GetData(string constr) {
    //'using' constructs are always a good idea when dealing with database operations
    //your connection will automatically close
    using(OleDbConnection con = new OleDbConnection(constr)){
        using(OleDbCommand cmd = new OleDbCommand()){       
            cmd.Connection = con;
            cmd.CommandText = "select * from tb1";
            cmd.CommandType = CommandType.Text;
            OleDbDataAdapter da = new OleDbDataAdapter();
            da.SelectCommand = cmd;
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds;
        }
    }
}
方法2-编程方式

private void BindWithDataBind() {
    dataGridView1.DataSource = GetData();
    //call the databind method to bound the data to the grid
    //the grid structure will be created automatically from the datatable structure
    dataGridView1.DataBind(); 
}

在MSDN关于DataGridView.Columns属性的官方文档中,您可以找到有关以编程方式绑定的更多信息。下面是答案。

Lucian的答案是正确的,在这种情况下,您应该使用数据绑定。但是,如果您想以另一种方式在DataGridView中添加行,下面是一个示例:

private void BindProgramatically() {

    //obtain the data from your OleDB connection
    DataSet ds = GetData(constr);
    if(ds == null) {
        return;
    }
    DataTable dt = ds.Tables[0];

    //build the grid structure, add a new grid column for each datatable column
    for(int i = 0; i < dt.Columns.Count; i++) {
        DataGridViewColumn newColumn = new DataGridViewColumn();
        newColumn.Name = dt.Columns[i].ColumnName;
        dataGridView1.Columns.Add(newColumn);
    }   

    for (int i = 0; i < dt.Rows.Count; i++) {
        //call ToArray to pass a copy of the data from the datatable
        //(make sure you have 'using System.Linq;' at the top of your file
        dataGridView1.Rows.Add(dt.Rows[i].ItemArray.ToArray());
    }
}

没有测试,但我认为它应该工作。只需对结果发表评论

为什么它的标题是“php”?对于这个问题,互联网上有大量令人难以置信的相关回复。请尝试谷歌:抱歉,它是c语言。我已经编辑了它,用这种方法查看了anser,我知道,但我现在的做法,你能帮我一下吗..如果可能的话,在这种情况下,只需注释dataGridView1.DataSource=ds.Tables[0]行,并在for循环中以编程方式完全绑定网格:它显示此错误。索引超出范围。必须为非负数且小于集合的大小。参数名称:第…行的索引dataGridView1.Rows[i]。Cells[j]。Value=ds.Tables[0]。Rows[i][j]。ToString;这是因为您尚未定义网格的柱结构。在for循环之前,您需要添加一个新的for循环,该循环遍历DataTable的列,并为数据表中的每一列添加一个新的DataGridViewColumn对象
private void BindProgramatically() {

    //obtain the data from your OleDB connection
    DataSet ds = GetData(constr);
    if(ds == null) {
        return;
    }
    DataTable dt = ds.Tables[0];

    //build the grid structure, add a new grid column for each datatable column
    for(int i = 0; i < dt.Columns.Count; i++) {
        DataGridViewColumn newColumn = new DataGridViewColumn();
        newColumn.Name = dt.Columns[i].ColumnName;
        dataGridView1.Columns.Add(newColumn);
    }   

    for (int i = 0; i < dt.Rows.Count; i++) {
        //call ToArray to pass a copy of the data from the datatable
        //(make sure you have 'using System.Linq;' at the top of your file
        dataGridView1.Rows.Add(dt.Rows[i].ItemArray.ToArray());
    }
}
private void Bind()
{
    OleDbConnection con = new OleDbConnection(constr);
    OleDbCommand cmd = new OleDbCommand();

    cmd.Connection = con;
    cmd.CommandText = "select * from tb1";
    cmd.CommandType = CommandType.Text;
    OleDbDataAdapter da = new OleDbDataAdapter();
    da.SelectCommand = cmd;
    DataSet ds = new DataSet();
    da.Fill(ds);


    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
        var row = (DataGridViewRow)dataGridView1.Rows[0].Clone();

        for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
        {
            row.Cells[j].Value = ds.Tables[0].Rows[i][j].ToString();
        }

        dataGridView1.Rows.Add(row);
    }

    con.Close();
}