locked
datatable to datagridview with columns RRS feed

  • Question

  • I have a datagridview with columns (A,B,C,D) as datagridview1

    I have a table with columns (A,B,C,D) as dt.

    Now the issue is when I fill the datagridview1 with datatable

    DataGridView1.DataSource = dt
    
    it will create datagridview1 with 8 columns ( A,B,C,D,A,B,C,D )
    & due to certain reasons I can not make the datagrid view without column.

    so please guide how to fill the data in already created column instead creating new one.


    Pradeep Yadav (Social MSDN)

    Saturday, September 8, 2012 5:53 AM

Answers

  • Found the Solution.

    Solution 1

    1.) Right Click DataGridView.
      2.) A Popup Menu appears and Click Edit Columns
      3.) Bind each columns (ID, Name, Quantity, other) by typing the field name (item_id, item_name, item_quantity) respectively from  your query in the DataPropertyName property (so that it will not  create another column like you did).

    Solution 2

    gridview1.Columns(0).DataPropertyName = "Length"

    Pradeep Yadav (Social MSDN)

    • Marked as answer by pk.yadav Monday, September 10, 2012 5:26 AM
    Monday, September 10, 2012 5:26 AM

All replies

  • I would try this way.

    1. You need to create a datatable and store all initial values.

    2. Get the second datatable.

    3. Use the merge method to copy the value of second datatable to the first

    datatableFirst = datatableFirst.Merge(datatableSecond, true);

    So you will have all the value in the first datatable.

    Now set the DataGridView1.DataSource = datatableFirst;

    Click here to know more about DataTable.Merge Method
     http://msdn.microsoft.com/en-us/library/wkk7s5zk.aspx


    Please mark this post as answer if it solved your problem. Happy Programming.

    • Proposed as answer by Kris.Moh Saturday, September 8, 2012 5:30 PM
    Saturday, September 8, 2012 5:30 PM
  • ...

    it will create datagridview1 with 8 columns ( A,B,C,D,A,B,C,D )

    which 4 columns are from datatable? 1st 4, or last 4? But dataTable only has 4 columns? 

    Do you want to create columns in dataTable as well? This would be the be idea, then all 8 columns would be bind to DGV.

    How to do it?

    Even if your dataTable has columns (and rows too) and is bind to DGV, you can still create new columns and add data to already existed rows:

    //note: this code bellow can be used after you bind datatable to datagridview!

    table.Columns.Add("A", GetType(String))
    table.Columns.Add("B", GetType(String))
    table.Columns.Add("C", GetType(String))
    table.Columns.Add("D", GetType(String))


    It will create new 4 columns inside DGV (with empty rows, of course).

    ---

    If this is not it, please tell us more about the issue - and be MORE precise, ok?!

    ok

    bye

    BTW Krishna: DO NOT mark your own posts as "Propopsed as answer"!!


    Mitja

    • Edited by Mitja Bonca Saturday, September 8, 2012 5:40 PM
    • Proposed as answer by Frank L. Smith Saturday, September 8, 2012 8:50 PM
    • Marked as answer by pk.yadav Sunday, September 9, 2012 5:26 PM
    • Unmarked as answer by pk.yadav Sunday, September 9, 2012 5:26 PM
    Saturday, September 8, 2012 5:38 PM
  • I DONT WANT TO CREAT 8 COLUMNS.

    WHAT I WANT IS THAT I HAVE CREATED 4 COLUMNS IN DGV AND WHEN I FILL THE DATE IN DGV WITH IT WILL FILL THE DATA IN ALREADY CREATED COLUMNS.

    FOR EG.

    COLUMN A OF DATATABLE FILL THE DATA IN COLUMN A OF DGVIEW.

    COLUMN B OF DATATABLE FILL THE DATA IN COLUMN B OF DGVIEW.

    COLUMN C OF DATATABLE FILL THE DATA IN COLUMN C OF DGVIEW.

    COLUMN D OF DATATABLE FILL THE DATA IN COLUMN D OF DGVIEW.

    NOW WHAT IS HAPENING, WHEN I FILL THE Datagridview with the datatable with this code.

    DataGridView1.DataSource = dt

    instead of filling the data in the already created columns it first create the new columns and fill the data in new columns..

    Pradeep Yadav (Social MSDN)

    Sunday, September 9, 2012 5:31 PM
  • Found the Solution.

    Solution 1

    1.) Right Click DataGridView.
      2.) A Popup Menu appears and Click Edit Columns
      3.) Bind each columns (ID, Name, Quantity, other) by typing the field name (item_id, item_name, item_quantity) respectively from  your query in the DataPropertyName property (so that it will not  create another column like you did).

    Solution 2

    gridview1.Columns(0).DataPropertyName = "Length"

    Pradeep Yadav (Social MSDN)

    • Marked as answer by pk.yadav Monday, September 10, 2012 5:26 AM
    Monday, September 10, 2012 5:26 AM