Reputation: 55
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.
Upvotes: 0
Views: 5196
Reputation: 7713
You have to add this on windows forms:
1. dgr_donors.AutoGenerateColumns = false;
2. dgr_donors.Columns.Add("id", "id");
3. dgr_donors.Columns.Add("BCode", "BarCode");
Upvotes: 0
Reputation: 583
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 AutoGenerateColumns
to false
in the DataGridView.
Then you have to add columns manually and specify DataField
values in each column equal to the real column name from SQL Server and HeaderText
equal to your desired display name of the column.
Now when you bind the DataTable
it will generate columns for the matching columns in the DataGridView
according to the DataField
and HeaderText
values.
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.
AutoGenerateColumns
is set to false
DataField
should be the field name in the database. HeaderText
should be your text.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="FirstName" HeaderText="First Name" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" />
<asp:BoundField DataField="DOB" HeaderText="Date Of Birth" />
</Columns>
</asp:GridView>
Upvotes: 3