Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7663103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:58:21+00:00 2026-05-31T13:58:21+00:00

QI want to display queried records from SQl Server which are in a table

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:58:22+00:00Added an answer on May 31, 2026 at 1:58 pm

    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.

    1. Note that AutoGenerateColumns is set to false
    2. 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>
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to display the TIME field from my mysql table on my website,
I am trying to display some information on a grid queried from a sql
I want to display tabular type data, but it will not be coming from
I have two tables users registered_members I want to confirm values from user table
I have the following issue: I want to display Tickets from a helpdesk system
I would like to know if the only way, in which we can display
So I want to display results from multiple queries from the same controller. I
I've mapped a class to a SQL Server table and use an SQL named
I have developped a HTTP web service which is queried by smartphone. I want
Effectively a duplicate of: How can I display data in table with Perl The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.