How to Bind specific Columns of a datatable to a DataGridView?

前端 未结 6 514
眼角桃花
眼角桃花 2020-12-09 11:43

My DataTable has three columns fetched from a database, while I need to bind only two columns of it to a DataGridView. Can you please help me with

6条回答
  •  天涯浪人
    2020-12-09 12:12

    private void Form1_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("connection string");
            SqlDataAdapter adp = new SqlDataAdapter("select  Fieldname1,fieldname2 from Table Name", con);
            DataSet ds = new DataSet();
            ds.Clear();
            adp.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                dataGridView1.DataSource = ds.Tables[0];
      }
    

    Definitely it will work.

提交回复
热议问题