【问题标题】:Add Insert new row between two existing rows in bounded DataGridView in C#在 C# 的有界 DataGridView 中的两个现有行之间添加插入新行
【发布时间】:2015-11-27 06:44:09
【问题描述】:

我尝试在 datagridview 中的两个现有行之间添加插入新的空白行(已绑定到数据库),但出现以下异常:

当控件绑定数据时,不能以编程方式将行添加到 DataGridView 的行集合中

我的代码

private void load()
{
    string strProvider = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\AMOL\Desktop\Maratha.accdb";
    string strSql = "Select * from Maratha";
    OleDbConnection con = new OleDbConnection(strProvider);
    OleDbCommand cmd = new OleDbCommand(strSql, con);
    con.Open();
    cmd.CommandType = CommandType.Text;
    OleDbDataAdapter da = new OleDbDataAdapter(cmd);
    DataTable scores = new DataTable();
    da.Fill(scores);
    DGMaratha.DataSource = scores;
}

public void BtnRefresh_Click(object sender, EventArgs e)
{
    try
    {
        int row = (this.DGMaratha.CurrentCell.RowIndex);
        int column = (this.DGMaratha.CurrentCell.ColumnIndex);
        load();
        this.DGMaratha.CurrentCell = this.DGMaratha[column, row];
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error: Failed to retrieve the required data from the DataBase.\n{0}", ex.Message);
        return;
    }
}

private void BtnAdd_Click(object sender, EventArgs e)
{
    int row = (this.DGMaratha.CurrentCell.RowIndex);
    DGMaratha.Rows.Insert(row, 1);
}

【问题讨论】:

    标签: c# winforms datagridview


    【解决方案1】:

    当 DataGridView 是数据绑定时,将项插入 DataSource:

    int idx = DGMaratha.CurrentCell.RowIndex;
    var table = (DataTable)DGMaratha.DataSource;
    var row = table.NewRow();
    table.Rows.InsertAt(row, idx);
    

    【讨论】:

    • 你好它的工作但问题是当我输入任何数据并点击保存按钮时它的保存但是我在中间添加的那个帐户它下降了,所以请帮助
    【解决方案2】:

    DGV,当绑定到 DB 时,更多的是显示而不是数组。如上所述,应该通过数据源修改来完成更改。

    【讨论】:

      猜你喜欢
      • 2016-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多