7

So as the title states, I'm trying to add rows to a DataGrid programmatically using C# but I can't seem to make it work. This is what I have so far.

// I have a DataGrid declared as dg in the XAML

foreach (string s in array) {
    int index = 0;
    DataGridRow dgRow = new DataGridRow();
    foreach (DataGridColumn dgColumn in columns)
    {
        // Trying to add Text to dgRow here but IDK how
        index++;
    }
}

I've been googling around and the closest I got to adding anything was to use {column = value} but it just throws me an error. Really out of ideas now though :\

2
  • Best way to use DataGridView to bound it with a DataSource(Datatable,dataset etc) and make changes in the datasource itself. Also in your code, you have to add dgRow in your datagridview to it be visible in UI. Commented Feb 10, 2015 at 7:11
  • How do I "bind" it to a DataTable exactly? Commented Feb 10, 2015 at 7:12

3 Answers 3

5

Here's you can do it better way by binding a source to datagridview

        // Creating DataSource here as datatable having two columns
        DataTable dt = new DataTable();
        dt.Columns.Add("ID", typeof(int));
        dt.Columns.Add("Name");

        // Adding the rows in datatable
        for (int iCount = 1; iCount < 6; iCount++)
        {
            var row = dt.NewRow();
            row["ID"] = iCount;
            row["Name"] = "Name " + iCount;
            dt.Rows.AddRow(row);
        }

        DataGridView dgv = new DataGridView();
        // Set AutoGenerateColumns true to generate columns as per datasource.
        dgv.AutoGenerateColumns = true;
        // Finally bind the datasource to datagridview.
        dgv.DataSource = dt;

In Case you are using WPF DataGrid you can bind it like this way-

dgv.DataContext = employeeData.DefaultView;

and in

XAML

<DataGrid Name="dgv" ItemsSource="{Binding}">
Sign up to request clarification or add additional context in comments.

6 Comments

Hi Rohit! Is there any way to get this to work with DataGrid? DataGridView doesn't seem to be available in my build of C#(?)
Hi, Rohit, I just realized DataGridView isn't available in WPF. I'm using WPF. Is there a way to make this work with WPF?
No it doesn't. I just tried and it refuses to even compile :\
As it turns out, all I was missing from making your code work was the ` ItemsSource="{Binding}"`hahaha :| THANK YOU SO MUCH!! I've been going crazy over this for a while now
glad that it helped you out. . .. .
|
2
//create datatable and columns,
DataTable dtable = new DataTable();
dtable.Columns.Add(new DataColumn("Column 1"));
dtable.Columns.Add(new DataColumn("Column 2"));

//simple way create object for rowvalues here i have given only 2 add as per your requirement
object[] RowValues = { "", "" };

//assign values into row object
RowValues[0] = "your value 1";
RowValues[1] = "your value 2";

//create new data row
DataRow dRow;
dRow = dtable.Rows.Add(RowValues);
dtable.AcceptChanges();

//now bind datatable to gridview... 
gridview.datasource=dbtable;
gridview.databind();

Regards

Comments

-1

did you try?:

int n=5; // number of rows you want to add
dataGridView1.Rows.Add(n);

// you can add (names of the rows) if you have them in your array
//for(int i=0; i<n; i++)
//dataGridView1[0, i].Value = array[i];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.