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?
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 MohantyPosted Feb 22, 2024, 5:40 AM
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.
Mehmet FatihPosted Feb 22, 2024, 12:39 PM
Thank you very much Amit.
Amit MohantyPosted Feb 22, 2024, 12:10 PM
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.
Mehmet FatihPosted Feb 22, 2024, 11:50 AM
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 MohantyPosted Feb 22, 2024, 11:33 AM
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 FatihPosted Feb 22, 2024, 11:06 AM
I am getting this result with your code, Amit. The result is smiliar to mine.
Pratik SomaiyaPosted Feb 22, 2024, 3:50 AM
This may happen because you are setting the DataSource first and then defining the DataGrid.
Can you try adding this statement post defining headers: