views:

411

answers:

2

I have a query which return rows with specific fields

SELECT First_Name, Midle_Name, Last_Name, Phone_home, Cell_home, ZipCode_Work, Phone_Work, Cell_Work FROM contact_info WHERE (Last_Name = @Last_Name)

and here is my code to bind this query to datagridview control.

protected void btnSearch_Click(object sender, EventArgs e) { DSSearchTableAdapters.contact_infoTableAdapter LastNameViewAdapter = new DSSearchTableAdapters.contact_infoTableAdapter(); DSSearch.contact_infoDataTable GetByLastName = LastNameViewAdapter.GetDataByLastNameView(txtSearch.Text); GridView1.DataSource = GetByLastName; GridView1.DataBind();

}

the problem that the datagridview will show all the fields in the table not the field I selected.

I'm suing VS 2008,asp.net with C# with mysql Database.

Can you help?

+1  A: 

Set AutoGenerateColumns to false and define the columns explicitly for the DataGridView object.

GridView1.Columns.Add(new System.Windows.Forms.DataGridViewColumn
 {
  HeaderText = "Column Header", 
  DataPropertyName = "ColumnName"
 });
Jake
where I should add this code?
Eyla
I'm getting this error :Error 5 The type or namespace name 'DataGridViewColumn' could not be found (are you missing a using directive or an assembly reference?) C:\Users\Ali\Documents\Visual Studio 2008\Projects\Imam_Contacts\Imam_Contacts\SearchPage.aspx.cs 45 39 Imam_Contacts
Eyla
A: 

In the design view of an aspx page, the grid view will have an image of a '<' in the upper right hand corner of the gridview, which is called a smart tag.
alt text Click on that and it will give you a menu.

From the menu select edit columns. That will bring up a Fields dialogue box. alt text In the bottom left of the dialogue box is a list of the columns that are in the gridview. You can select any of the columns in the list and delete them by clicking the red X next to the list. You can also use the up and down arrows to arrange the order of the columns in the gridview.

awright18