简体   繁体   中英

Adding row to binded datagridview

I want to add a stored CustomDataGridViewRow into a DataGridView which is binded. Like follows:

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;

Thought it goes this way:

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());            
    }
}

But it only shows me DataGridViewRow { Index=1 } in first cell.

solved it

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);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM