简体   繁体   English

无法添加自定义列标题

[英]Unable to add Column Headers Custom

在此处输入图片说明 Good Day.. I have a gridview and it is showing columns as shown in diagram above 美好的一天..我有一个gridview,它显示如上图所示的列

I would like to replace column names from ID Name Task Total Sale etc 我想从ID名称任务总计销售等中替换列名称

But I am unable to do it 但是我做不到

When I give header text, or edit column, it adds the rows from database NOT below the header text 当我提供标题文本或编辑列时,它会在标题文本下方添加数据库中的行

Please Guide 请指导

Thanks 谢谢

This is what Search button does when clicked: 单击时,这是“搜索”按钮的作用:

private void button3_Click_1(object sender, EventArgs e)
    {

        SqlConnection  strg = new SqlConnection("Data Source=RANA;Initial Catalog=PlacementCellProject;Integrated Security=True;Pooling=False");
        //SqlConnection cn = new SqlConnection(strg);
        SqlDataAdapter sdf = new SqlDataAdapter("select ID, name, task, total_sale, owner, worker, tdate, payment_status from saloonworkers where tdate between '" + dateTimePicker2.Value.ToString() + "' and '" + dateTimePicker3.Value.ToString() + "' ", strg);
        DataTable sd=new DataTable();
        sdf.Fill(sd);
        dataGridView1.DataSource=sd;
        SqlDataReader dr;
    }

And this is OnLoad initializatoin: 这是OnLoad初始化:

    public Form6()
    {
        InitializeComponent();
        textBox3.Text = "";
        showworkers();
        ownershare();
        workershare();
        dataGridView1.Columns[0].HeaderText = "I";

       //  dataGridView1.Columns["ID"].HeaderText = "I";
      /*dataGridView1.Columns.Add("ID", "I");

        for (int i = 0; i < dataGridView1.Columns.Count; i++)
        {
            string header = dataGridView1.Columns[i].HeaderText;
        }*/

    }

If you binding data you can use property "DisplayName". 如果绑定数据,则可以使用属性“ DisplayName”。

[DisplayName("Name")]

public string FirstName {get;set;}

Also you can onload page put header text like this 您也可以像这样在onload页面上放置标题文本

grid.Columns[0].HeaderText = "Name";

OK, you try first in OnLoad method change headers which not exist, add your code from button to onload and next change yours headers. 好的,您首先尝试在不存在的OnLoad方法更改标头中,将代码从按钮添加到onload,然后更改您的标头。 FIRST add datasource next change headers. 首先添加数据源,然后再更改标头。

After binding the DataSource to DataGridView . DataSource绑定到DataGridView Try change the HeaderText . 尝试更改HeaderText

sampleGridView.DataSource = stu;
sampleGridView.Columns[0].HeaderText = "ID";
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="ContactName" HeaderText="Contact Name" ItemStyle-Width="150px" />
        <asp:BoundField DataField="City" HeaderText="City" ItemStyle-Width="100px" />
        <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="100px" />
    </Columns>
</asp:GridView>

you will set column name before databind. 您将在数据绑定之前设置列名称。 i hope it's help you 希望对您有帮助

private void BindGrid()
        {
            string constring = @"Data Source=.\SQL2005;Initial Catalog=Northwind;User id = sa;password=pass@123";
            using (SqlConnection con = new SqlConnection(constring))
            {
                using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", con))
                {
                    cmd.CommandType = CommandType.Text;
                    using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                    {
                        using (DataTable dt = new DataTable())
                        {
                            sda.Fill(dt);

                            //Set AutoGenerateColumns False
                            dataGridView1.AutoGenerateColumns = false;

                            //Set Columns Count
                            dataGridView1.ColumnCount = 3;

                            //Add Columns
                            dataGridView1.Columns[0].Name = "CustomerId";
                            dataGridView1.Columns[0].HeaderText = "Customer Id";
                            dataGridView1.Columns[0].DataPropertyName = "CustomerID";

                            dataGridView1.Columns[1].HeaderText = "Contact Name";
                            dataGridView1.Columns[1].Name = "Name";
                            dataGridView1.Columns[1].DataPropertyName = "ContactName";

                            dataGridView1.Columns[2].Name = "Country";
                            dataGridView1.Columns[2].HeaderText = "Country";
                            dataGridView1.Columns[2].DataPropertyName = "Country";
                            dataGridView1.DataSource = dt;
                        }
                    }
                }
            }
        }

Please use given code.i am sure it's help you. 请使用给定的代码。我相信它会对您有所帮助。

首先,您需要加载数据源,然后更改标题文本

sampleGridView.Columns[0].HeaderText = "ID";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM