Reputation: 5851
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?
Upvotes: 0
Views: 2063
Reputation: 2333
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 http://www.freeimagehosting.net/uploads/5dd1c59fe9.jpg
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 http://www.freeimagehosting.net/uploads/8aafd8a14a.jpg 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.
Upvotes: 0
Reputation: 7773
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"
});
Upvotes: 1