【问题标题】:Adding row to binded datagridview将行添加到绑定的datagridview
【发布时间】:2012-02-15 19:25:37
【问题描述】:

我想将存储的CustomDataGridViewRow 添加到绑定的DataGridView 中。如下:

CustomDataGridViewRow rowTemplate = new CustomDataGridViewRow();
dataGridView1.RowTemplate = rowTemplate;

Datenbank.cmd = 
    new SqlCommand("[Terminauswertung_Bericht_Gesamt]", Datenbank.connection);
Datenbank.cmd.CommandType = CommandType.StoredProcedure;
Datenbank.cmd.Parameters.AddWithValue("@berichtsnr", 1);

SqlDataAdapter adapter = new SqlDataAdapter(Datenbank.cmd);
dataSet1.Tables.Clear();
adapter.Fill(dataSet1, "Table");
bs = new BindingSource();
bs.DataSource = dataSet1.Tables["Table"];
dataGridView1.DataSource = bs;

以为是这样的:

dataSet1.Tables[0].Rows.Add(Cache.getRow(1));

public class cache
{
    Dictionary<int, CustomDataGridViewRow> _cache = 
        new Dictionary<int, CustomDataGridViewRow>();

    public CustomDataGridViewRow getRow(int index)
    {
        foreach (KeyValuePair<int, CustomDataGridViewRow> dic in _cache)
        {
            if (dic.Key == index)
                return (dic.Value);
        }
        return (new CustomDataGridViewRow());            
    }
}

但它只在第一个单元格中显示 DataGridViewRow { Index=1 }。

【问题讨论】:

  • 您介意发布您的解决方案作为您自己问题的答案吗? (并接受它)。这样做完全没问题。
  • @GertArnold 好的,我以后会这样做,不知道它可以因为这个:“低于 100 名声望的用户在询问后 8 小时内无法回答他们自己的问题。你可以3 小时后自行回答。在此之前,请使用 cmets,或改为编辑您的问题。 3小时内搞定

标签: c# datagrid datagridview


【解决方案1】:

解决了

DataRow newRow =test.Tables[0].NewRow();


newRow.ItemArray = Cache.getRowValues(child);
 test.Tables[0].Rows.InsertAt(newRow, c.Index+1);

 public string[] getRowValues(int index)
        {
            List<string> temp = new List<string>();
            foreach (KeyValuePair<int, CustomDataGridViewRow> dic in _cache)
            {
                if (dic.Key == index)
                {
                    foreach (DataGridViewCell cell in dic.Value.Cells)
                        temp.Add(cell.Value.ToString());
                }
            }
            string[] result = temp.ToArray();

            return (result);
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-26
    • 1970-01-01
    • 2017-05-01
    相关资源
    最近更新 更多