正在将行添加到绑定的数据网格视图
本文关键字:数据 数据网 网格 视图 绑定 添加 | 更新日期: 2023-09-27 18:24:57
我想将存储的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}。
解决了
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);
}