Reputation: 57
Good Day..
I have a gridview and it is showing columns as shown in diagram above
I would like to replace column names from ID Name Task Total Sale etc
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:
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;
}*/
}
Upvotes: 0
Views: 409
Reputation:
First you need load datasource and next change headertext
sampleGridView.Columns[0].HeaderText = "ID";
Upvotes: 0
Reputation: 1024
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.
Upvotes: 0
Reputation: 6203
If you binding data you can use property "DisplayName".
[DisplayName("Name")]
public string FirstName {get;set;}
Also you can onload page put header text like this
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. FIRST add datasource next change headers.
Upvotes: 1
Reputation: 1024
<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
Upvotes: 0
Reputation: 2978
After binding the DataSource
to DataGridView
. Try change the HeaderText
.
sampleGridView.DataSource = stu;
sampleGridView.Columns[0].HeaderText = "ID";
Upvotes: 0