1

I Have requirement to fill the datagridview row by row . i.e. if 3rd row is currently selected then data needs to be filled in 3rd row (my query returns always single row ) . same for every row.

some what like

              DataTable dt = new DataTable();
        dataadapter.Fill(dt);

        dataGridView1.Rows[index].DataSource = dt; (just a hunch, but not working)
   (instead of )  //dataGridView1.DataSource = dt;

hope that I made my requirement clear to you

Is their any way to accomplish it .... Thanks in advance....

speedyraz
  • 335
  • 1
  • 5
  • 18

2 Answers2

1

If your query returns a single row then you just have to fill the columns with correct values from your query:

dataGridView1.Rows[rowIndex].Cells["ColumnName"].Value = 
    dt.Rows[0]["ColumnNameInDataTable"].ToString();

Your grid will not be bound to the data this way. But it will fill the columns in the current row like you've asked.

jaredbaszler
  • 3,941
  • 2
  • 32
  • 40
  • thanks ... lots of thanks .. you just saved my whole work ... your answer is exactly what I am searching since many days .....thanks again .. – speedyraz Apr 14 '17 at 04:40
0

I don't think this is possible directly, You could use a List with a certain amount of empty objects and bind this List to your DataGridView... Then you would have empty Rows to work with. You can then fill single rows of the List with data and update the binding. I also think you need a BindingSource and not a DataTable in this case...

Something like this:

private List<YourObject> gridData;          // List
private BindingSource bindingSource;   

public void Init()
{
   this.gridData = new List<Anything>();
   //prefill list, in this case we want to have 100 empty rows in DataGrid
   for (var i = 0; i < 100; i++)
   {
      this.gridData.Add(new YourObject());
   }
   this.bindingSource.DataSource = this.gridData;
}

public void UpdateRow(int row)
{
    this.gridData[row] = (from .. in select ...).FirstOrDefault(); // your query
}