Kamran
Kamran

Reputation: 4100

Select particular column from Datatable and bind with Data GridView C#, Windows form Application

I am saving some data in Data table dt_Companies.

sda = new SqlDataAdapter(" Select ID,Company_Name,Zip,City,Street" +
                         " From Companies " +
                         " WHERE ( " + strr + " ) ", conn);

sda.Fill(dt_Companies);

I know to show this data in data grid view dataGridView_Companies. I have to use this

dataGridView_Companies.DataSource = dt_Companies;

But I only want to select Company_Name,Zip,City,Street from data table dt_Companies. I need the ID field later on that's why I cannot skip it.

I am looking some thing like this:

dataGridView_Companies.DataSource = dt_Companies.Select("Company_Name","Zip","City","Street"); 

Upvotes: 0

Views: 4366

Answers (2)

vutran
vutran

Reputation: 404

So you'd like to display columns Company_Name,Zip,City,Street and hide columns ID ? If so, after binding data, you can choose whether to hide column name ID from DataGridView. See How to: Hide Columns in DataGridView Controls

Upvotes: 2

Plue
Plue

Reputation: 1790

You can use BoundField in your asp. Look here.

Just bind the columns you want :

<columns>
    <asp:boundfield datafield="CompanyName"
            convertemptystringtonull="true"
            headertext="Customer Name"/>
     <asp:boundfield datafield="Zip"
            convertemptystringtonull="true"
            headertext="Zip"/>
     <asp:boundfield datafield="City"
            convertemptystringtonull="true"
            headertext="City"/>
     <asp:boundfield datafield="Street"
            convertemptystringtonull="true"
            headertext="Street"/>
</columns>

Upvotes: 0

Related Questions