i have a gridview that displays query data from 1 table after the user presses a button with a code behind query, this gridview had autogeneratecolumns = false and i added the BoundField DataField header texts myself, for example, the first column in the db had the name "prd_nome" and i changed it to "nome", everything worked as it was supposed to.
now i've created a another table in the db, with a different name and different column names as well, i've also added another button with a code behind query to get data from that table but in order to display the data now the autogeneratecolumns = true, i tested the button and it works, however the header texts are the same as the column names and the ID column is also showing for both queries.
how do i hard code the header texts to what i want and how do i hide the ID column? via labels? via boundfield datafields? via the AS sql operator? if anyone could help me, i would appreciate because i'm a c# novice
here's the current gridview asp code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"></asp:GridView>
here's the code of the button that passes a query from the first table:
protected void Button1_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
SqlConnection connection = new SqlConnection(GetConnectionString());
connection.Open();
SqlCommand sqlCmd = new SqlCommand("SELECT * FROM [ERPDQ].[dbo].[prd] WHERE prd_desc LIKE ('torta%')", connection);
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
sqlDa.Fill(dt);
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
connection.Close();
}
and here's the code from the button that passes a query regarding the second table:
protected void Button6_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
SqlConnection connection = new SqlConnection(GetConnectionString());
connection.Open();
SqlCommand sqlCmd = new SqlCommand("SELECT * FROM [ERPDQ].[dbo].[outra]", connection);
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
sqlDa.Fill(dt);
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
connection.Close();
}
}
When using AutoGenerateColumns=True you can change the header texts of columns by using AS sql operator in your select statement (as you already suggested).
Although in general avoid using AutoGenerateColumns=True, because its inflexible. As you pointed out you get stuck by easy things such as hiding one column.
And here is an example how to hide ID column by using RowDataBound
event
protected void rowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
for (int i = 0; i < e.Row.Controls.Count; i++)
{
var headerCell = e.Row.Controls[i] as DataControlFieldHeaderCell;
if (headerCell != null)
{
if (headerCell.Text == "name_of_id_column")
{
headerCell.Visible = false;
Page.Items["IDCellIndex"] = i;
break;
}
}
}
}
else if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Footer)
{
int idCellIndex = Convert.ToInt32(Page.Items["IDCellIndex"]);
e.Row.Controls[idCellIndex].Visible = false;
}
}