簡體   English   中英

如何將自定義列標題設置為 datagridview 並顯示數據表記錄?

[英]How to set custom column header to a datagridview and display datatable records?

問我想顯示來自 SQL Server 的查詢記錄,這些記錄位於具有自定義列標題的表中。 我該怎么做 ?

我的查詢功能如下

        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;

    }

為了在 datagridview 中綁定和顯示,我在顯示按鈕單擊事件上有以下內容。

 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();           
    }
       }

我希望返回的記錄在列標題下顯示為 Sl。 不,姓名和注冊。 數字。 我該怎么做 ? 請幫助提供代碼示例。

我猜您無法更改 SQL 查詢以按照您想要的方式獲取列名。 所以讓我們修改DataGridView。

如果您看到 DataTable 中的列名與 DataGridView 中的列名一樣,那是因為它會自動生成它們。

如果您想在列中使用自己的標題文本,則必須自己指定它們。

第一,您需要關閉列自動生成。 為此,在 DataGridView 中將AutoGenerateColumns設置為false

然后,您必須手動添加列並在每列中指定DataField值等於來自 SQL Server 的實際列名和HeaderText等於您想要的列顯示名稱。

現在,當您綁定DataTable ,它將根據DataFieldHeaderText值為DataGridView的匹配列生成列。

在此處查看示例以獲取示例標記: http : //msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.columns.aspx

編輯:

這是一個示例 GridView 標記,用於展示您必須如何手動設置列。

  1. 請注意AutoGenerateColumns設置為false
  2. DataField應該是數據庫中的字段名稱。 HeaderText應該是您的文本。

     <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>

您必須在 Windows 窗體上添加此內容:

            1. dgr_donors.AutoGenerateColumns = false;
            2. dgr_donors.Columns.Add("id", "id");
            3. dgr_donors.Columns.Add("BCode", "BarCode");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM