I want to change the headertext of the data I pulled into the datagridview . However, the headertext in the last column interestingly does not change. I couldn't figure out why. What do you think the reason is?
C#
cmd1 = new OleDbCommand("SELECT okulno,tcno,adisoyadi,dtarihi,cinsiyeti,sinif,velicep,veliev,adres FROM ogrencibilgi order by sinif asc,okulno asc", conn);
adp1 = new OleDbDataAdapter(cmd1);
ds = new DataSet();
adp1.Fill(ds, "ogrencibilgi");
dataGridView1.DataSource = ds;
ds.Tables[0].Columns["okulno"].ColumnName = "Okul No";
ds.Tables[0].Columns["tcno"].ColumnName = "T.C. NO";
ds.Tables[0].Columns["adisoyadi"].ColumnName = "ADI-SOYADI";
ds.Tables[0].Columns["dtarihi"].ColumnName = "D. TARIHI";
ds.Tables[0].Columns["cinsiyeti"].ColumnName = "CINSIYETI";
ds.Tables[0].Columns["sinif"].ColumnName = "SINIFI";
ds.Tables[0].Columns["velicep"].ColumnName = "VELI CEP";
ds.Tables[0].Columns["veliev"].ColumnName = "EV TEL";
ds.Tables[0].Columns["adres"].ColumnName = "ADRES";
dataGridView1.DataMember = "ogrencibilgi";
Amit MohantyRank #112 · 20K reputationPosted Feb 22, 2024, 12:40 AM EST
The issue might be related to the data binding process. When you set the DataSource property of the dataGridView1, the grid is bound to the data source, and the column headers are typically automatically generated based on the column names from the data source. You should set the column names before setting the DataSource for your DataGridView.
cmd1 = new OleDbCommand("SELECT okulno,tcno,adisoyadi,dtarihi,cinsiyeti,sinif,velicep,veliev,adres FROM ogrencibilgi order by sinif asc,okulno asc", conn); adp1 = new OleDbDataAdapter(cmd1); ds = new DataSet(); adp1.Fill(ds, "ogrencibilgi"); ds.Tables[0].Columns["okulno"].ColumnName = "Okul No"; ds.Tables[0].Columns["tcno"].ColumnName = "T.C. NO"; ds.Tables[0].Columns["adisoyadi"].ColumnName = "ADI-SOYADI"; ds.Tables[0].Columns["dtarihi"].ColumnName = "D. TARIHI"; ds.Tables[0].Columns["cinsiyeti"].ColumnName = "CINSIYETI"; ds.Tables[0].Columns["sinif"].ColumnName = "SINIFI"; ds.Tables[0].Columns["velicep"].ColumnName = "VELI CEP"; ds.Tables[0].Columns["veliev"].ColumnName = "EV TEL"; ds.Tables[0].Columns["adres"].ColumnName = "ADRES"; dataGridView1.DataSource = ds; dataGridView1.DataMember = "ogrencibilgi";Mehmet FatihRank #1,474 · 1.1K reputationPosted Feb 22, 2024, 6:50 AM EST
Thanks Amit. I have foun out the problem. It was giving this error when the column name and header name were the same. When changing the title to a different name, I fixed it. Is there a way to disable columns we don't use? I store it with the false property as follows, but since there are too many columns, I have difficulty using the false property one by one. Thank you for your interest.
dataGridView1.Columns["velimail"].Visible = false;
Amit MohantyRank #112 · 20K reputationPosted Feb 22, 2024, 7:10 AM EST
You can iterate through the columns of the DataGridView and set the visibility property to false for the ones you don't want to display.
foreach (DataGridViewColumn column in dataGridView1.Columns) { // List the columns you want to hide if (column.Name == "velimail" || column.Name == "another_column_name") { column.Visible = false; } }Pratik SomaiyaRank #1,184 · 1.5K reputationPosted Feb 21, 2024, 10:50 PM EST
This may happen because you are setting the DataSource first and then defining the DataGrid.
Can you try adding this statement post defining headers:
dataGridView1.DataSource = ds.Tables[0];Mehmet FatihRank #1,474 · 1.1K reputationPosted Feb 22, 2024, 6:06 AM EST
I am getting this result with your code, Amit. The result is smiliar to mine.
Amit MohantyRank #112 · 20K reputationPosted Feb 22, 2024, 6:33 AM EST
If you're still encountering the same issue, navigate to your dataGridView1 properties, then go to Edit Columns. Check the HeaderText property of the last two columns. Ensure that you have specified the column HeaderText as 'CINSIYETI' and 'ADRES'.
Mehmet FatihRank #1,474 · 1.1K reputationPosted Feb 22, 2024, 7:39 AM EST
Thank you very much Amit.