Share via


How do i map the columns of the returning DataTable to the columns of the DataGridView control?

Question

Sunday, February 7, 2010 6:45 AM

Hi friend,

My DAL methods returns a DataTable with six columns. But my DataGridView control has to display only fours columns and those columns were added to the DataGridView control at desigin times...therefore i need to map each columns of the return table to the columns of the DataGridView control. How do i do this?

 

thanks

 

All replies (3)

Sunday, February 7, 2010 7:04 AM ✅Answered

Set the DataProperty on the Column to the name of the field in your datasource. You can find that in the properties of each field in your DataGridView
Dewayne Dodd - Landshark Software "Please make sure to 'Mark As Answer' if this answer solves your question"


Wednesday, February 10, 2010 6:15 AM ✅Answered

Hi code_warrior,

When the AutoGenerateColumns property of the datagridview is set to true, each column automatically sets its DataPropertyName property to the name of a property or database column in the data source specified by the DataSource property. This binding can also be performed manually, which is useful when you want to display only a subset of the properties or database columns available in the data source. In such cases, set the AutoGenerateColumns property to false, setting the value of each DataPropertyName property to the properties or database columns in the data source that you want to display. Please refer to the following code:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private DataTable dt = new DataTable();
    private void Form1_Load(object sender, EventArgs e)
    {
        dt.Columns.Add("dtcol1");
        dt.Columns.Add("dtcol2");
        dt.Columns.Add("dtcol3");
        dt.Columns.Add("dtcol4");
        dt.Columns.Add("dtcol5");
        dt.Columns.Add("dtcol6");

        DataRow dw;
        for (int icnt = 0; icnt < 5; icnt++)
        {
            dw = dt.NewRow();
            dw["dtcol1"] = "value1" + icnt;
            dw["dtcol2"] = "value2" + icnt;
            dw["dtcol3"] = "value3" + icnt;
            dw["dtcol4"] = "value4" + icnt;
            dw["dtcol5"] = "value5" + icnt;
            dw["dtcol6"] = "value6" + icnt;
            dt.Rows.Add(dw);
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        dataGridView1.AutoGenerateColumns = false;
        dataGridView1.Columns[0].DataPropertyName = "dtcol1";
        dataGridView1.Columns[1].DataPropertyName = "dtcol3";
        dataGridView1.Columns[2].DataPropertyName = "dtcol4";
        dataGridView1.Columns[3].DataPropertyName = "dtcol6";
        dataGridView1.DataSource = dt;
    }
}

Best regards,
Alex Liang

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.


Thursday, July 25, 2013 3:27 PM

Hi!

An approach I have used is to create a new datatable and leave out the columns I dont want in the DataGridView .

I can then use the same DataGridView to display all kind of tables and get a nice separation.

Of course it depends on the application wheter You can or should do it this way.