Datagrid view in Winform


I have created a datagridview in my winform.

I have part created it using the IDE (see image 1) and would like to update some of it from the database.
I can't see how I tell the datagridview to update the columns I want it to.

1. How do I update my 2 columns from the SQL into the first to columns of my datagrid?

Here is the code that I have so far but this just adds the columns from the database on the end of the datagrid which I do not want.

string cs = @WhichSystem();
        string sql = "SELECT Customer_Code,Description from MCRS_LU_Products where Product_Group like '01'"; //sql statment to display all data
        OleDbConnection conn = new OleDbConnection(cs); //connectiion
        OleDbDataAdapter da = new OleDbDataAdapter(sql, conn); //data adapter object
        DataSet ds = new DataSet(); //dataset object to keep data in table
        conn.Open(); //open connection
        da.Fill(ds, "table_name"); // fill the dataset with table_name through data adapter
        conn.Close(); //close connection
        dgCleanseData.DataSource = ds; //populate the datagridview with dataset
        dgCleanseData.DataMember = "table_name"; // populate datagridview with table_name

Open in new window

I would like:
a) Customer_Code to populate the column 'EAN' on the datagrid
b) Description to populate the 'Description' column on the datagrid.

Thank you
Who is Participating?
Robert SchuttSoftware EngineerCommented:
You did not attach an image but I assume you just defined the columns you want in the IDE like this:
captureI think you can get what you want by inserting this code before you assign the DataSource (between line 8 and 9):
            dgCleanseData.AutoGenerateColumns = false;
            dgCleanseData.Columns[0].DataPropertyName = "Customer_Code";
            dgCleanseData.Columns[1].DataPropertyName = "Description";

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.