QI want to display queried records from SQl Server which are in a table with custom column headers. How can I do it ?
My querying function is as below
private DataTable GetRecords(int QID, DateTime FromDate, DateTime ToDate)
{
SqlConnection myConn = new SqlConnection(ConfigurationManager.ConnectionStrings["CONNSTRING"].ConnectionString);
// Create the command and set its properties.
SqlCommand command = new SqlCommand();
command.Connection = myConn;
command.CommandText = "sp_GetRecords";
command.CommandType = CommandType.StoredProcedure;
// Add the input parameter and set its properties.
SqlParameter parameter1 = new SqlParameter();
parameter1.ParameterName = "@QID";
parameter1.SqlDbType = SqlDbType.Int;
parameter1.Direction = ParameterDirection.Input;
parameter1.Value = QID;
SqlParameter parameter2 = new SqlParameter();
parameter2.ParameterName = "@FromDate";
parameter2.SqlDbType = SqlDbType.DateTime;
parameter2.Direction = ParameterDirection.Input;
parameter2.Value = FromDate;
SqlParameter parameter3 = new SqlParameter();
parameter3.ParameterName = "@ToDate";
parameter3.SqlDbType = SqlDbType.DateTime;
parameter3.Direction = ParameterDirection.Input;
parameter3.Value = ToDate;
// Add the parameter to the Parameters collection.
command.Parameters.Add(parameter1);
command.Parameters.Add(parameter2);
command.Parameters.Add(parameter3);
myConn.Open();
SqlDataReader GetAgentTransactions = command.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(GetAgentTransactions);
GetAgentTransactions.Close();
myConn.Close();
return dt;
}
To bind and display in datagridview, I have the following on a display button click event.
protected void btnShow_Click(object sender, EventArgs e)
{
DataTable table = new DataTable();
table = GetRecords(1, Convert.ToDateTime(txtFromDate.Text.Trim()), Convert.ToDateTime(txtToDate.Text.Trim()));
datagridview.DataSource = table;
datagridview.DataBind();
}
}
I want the returned records to be shown under the column headers as Sl. No, Name and Reg. Number. How can I do it ? Please help with a code example.
I guess you can’t change the SQL queries to get column names the way you want. So let’s modify the DataGridView.
If you see the column names from the DataTable as it is in the DataGridView as columns it is because it auto-generates them.
If you want your own header text in columns you have to specify them yourself.
1st you need to turn off column auto-generation. For that set
AutoGenerateColumnstofalsein the DataGridView.Then you have to add columns manually and specify
DataFieldvalues in each column equal to the real column name from SQL Server andHeaderTextequal to your desired display name of the column.Now when you bind the
DataTableit will generate columns for the matching columns in theDataGridViewaccording to theDataFieldandHeaderTextvalues.Checkout the Example here for sample markup: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.columns.aspx
EDIT:
Here’s a sample GridView markup to show how you have to manually setup columns.
AutoGenerateColumnsis set tofalseDataFieldshould be the field name in the database.HeaderTextshould be your text.