Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to bind a DataTable selected columns to a DataGridView in Windows Forms

How to bind a DataTable selected columns to a DataGridView in Windows Forms

Scheduled Pinned Locked Moved C#
c++winformstutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pavanip
    wrote on last edited by
    #1

    Hi, I want to bind only one column, from the collection of columns of the DataTable table, to a DataGidView.I tried with this code for (int c = 0; c <= i; c++) { dataGridView1.DataSource = dt1.Rows[i,1].ToString(); dataGridView1.Refresh(); } but Nothing is displayed in dataGridView1. Please reply me if anyone know the solution.

    W N 2 Replies Last reply
    0
    • P pavanip

      Hi, I want to bind only one column, from the collection of columns of the DataTable table, to a DataGidView.I tried with this code for (int c = 0; c <= i; c++) { dataGridView1.DataSource = dt1.Rows[i,1].ToString(); dataGridView1.Refresh(); } but Nothing is displayed in dataGridView1. Please reply me if anyone know the solution.

      W Offline
      W Offline
      Wendelius
      wrote on last edited by
      #2

      You don't loop through the rows. Instead you define a datasource for the DataGridView, create a column in it and define the column from the datatable to show. For example something like this:

      DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
      column.DataPropertyName = "ColumnInDataTable";
      column.HeaderText = "Header";
      column.Name = "somename";
      //
      this.dataGridView1.Columns.Add(column);
      this.dataGridView1.DataSource = dt1;

      Of course you are aware that you can do this also in the designer.

      The need to optimize rises from a bad design.My articles[^]

      1 Reply Last reply
      0
      • P pavanip

        Hi, I want to bind only one column, from the collection of columns of the DataTable table, to a DataGidView.I tried with this code for (int c = 0; c <= i; c++) { dataGridView1.DataSource = dt1.Rows[i,1].ToString(); dataGridView1.Refresh(); } but Nothing is displayed in dataGridView1. Please reply me if anyone know the solution.

        N Offline
        N Offline
        Najmal
        wrote on last edited by
        #3

        Hi....... U have lots of options to do this.... First One: By Using BindingSource:::>> dataGridView1:>properties:>Choose Datasource:>{If u have already one then select it otherwise select Add Project Datasource} If u cann't follow this Just message me!!!!!! Second One: fill dataset according to ur needs.... DataView dv = new DataView(dt.Tables["Specify table name here"]); dataGridView1.DataSource = dv; dataGridView1.Refresh(); {Remember Donn't Add Columns to DataGridView } try this...... and reply me....

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups