adding rows programmatically to an unbounded datagridview

前端 未结 3 1577
眼角桃花
眼角桃花 2021-01-29 04:01

I\'m sending values from one form to another form, then want to display in dgv,

I\'m trying this, there is no error during execution, bt it does not show data in dgv..

相关标签:
3条回答
  • 2021-01-29 04:35

    You're close.

    What you can do is use the return value of your call to DataGridViewRowCollection.Rows.Add() method, which is the index value of the just added row.

    Change your code to this:

    int RowIndex = lineItemsDGV.Rows.Add();
    DataGridViewRow NewRow = lineItemsDGV.Rows[RowIndex];
    NewRow.Cells[0].Value = item.Product_id;
    NewRow.Cells[1].Value = item.product_name;
    NewRow.Cells[2].Value = item.specification;
    NewRow.Cells[3].Value = item.unit_price;
    NewRow.Cells[4].Value = item.unit_price * 1;
    
    0 讨论(0)
  • 2021-01-29 04:36

    This is how I would do it:

    DataRow rowToAdd= myTable.NewRow(); 
    
    rowToAdd["ProductId"] = item.Product_id;
    rowToAdd["ProductName"] = item.product_name;
    rowToAdd["Specification"] = item.specification;
    rowToAdd["UnitPrice"] = item.unit_price;
    
    myTable.Add(rowToAdd);
    

    And bind the datatable to the gridview.

    0 讨论(0)
  • 2021-01-29 04:46

    I know this thread is old, but I found this page helpful today and this is how I accomplished adding a new row which is different than the previous solutions.

    Assumption: datagridview has columns specified at design time. In my case I had 3 text columns.

    The solution is to use the DataGridViewRowCollection.Add functionality and pass a parameter array that contains a list of each column value separated by a comma.

    DataGridView1.Rows.Add("Value 1", "Value 2", "Value3")
    

    Using this approach, there's no need to use the BeginEdit, Update or Refesh dgv functions.

    0 讨论(0)
提交回复
热议问题