Editing datagridview column properties programmatically

LabProARW

Well-known member
Joined
Nov 4, 2019
Messages
52
Programming Experience
1-3
The background is this... I have a tab control on a form which has multiple tabs. In order to get the appropriate category of Method_Titles on each tab, I have had to programmatically set up the data binding etc.. as the data comes from an Access backend. For each tab there is a SELECT statement returning the desired category. OK. So what I see is that even though my form loads/displays the correct category Method_Titles - I cannot do any formatting through Designer for the columns headers attributes - because there is no template table looking header structure of my datagridview to click on and modify size, name properties in Designer to click on.

My attempts to edit the table column headers of my datagridview only work if I am adding the column first. The columns and data display already when I run the app - but I want to modify the datagridview column properties and that cannot be easily done at run time. Since the data is displayed (not like I want), but nevertheless displayed... it doesn't seem like I should have to programmatically add each and every column in order to modify properties. It would be real nice if I could refer to and modify the column properties of what already is getting displayed.

Any code snippet or help on how to do this?
 
Alakazam I can see your code to understand your riddle.

Please ask a specific question. Or show what you tried. Or both.
 
but I want to modify the datagridview column properties and that cannot be easily done at run time. Since the data is displayed (not like I want), but nevertheless displayed... ... It would be real nice if I could refer to and modify the column properties of what already is getting displayed.
And what's wrong with accessing the DataGridView.Columns collection and then changing the properties of the columns found there at runtime?

I find your question kind of interesting because a few years ago, almost everybody who use the AutoGenerateColumns figured out on their own to access the Columns collection to modify how the automatically generated columns and the typical question is how to change the "type" of column that was generated. In your case, you didn't even discover that. Kind of unusual unless you are the drag-n-drop type VB6 programmer who doesn't even look at the documentation.
 
I should have made it clear I am trying to teach myself C# and have used VBA and a little VB.NET in the past. Even clearly formulating the question in a language that experienced users converse in is a challenge. In Designer I had dropped a DataGridView object on a tab control. There are no visible column headers on the new DGV in Designer I guess since the data source has not been connected yet.

I found code to populate the DGV using the code below. When my application runs I see the data in the datagridview from the data source ACCESS database back end table... however when I try to modify the dataGridViewALL.Columns property in any way I get the Null reference exception. I would guess that the dataGridViewALL which populates if I don't try to change anything, simply does not exist yet at this point where I try to modify the columns that I thought existed somewhere based upon my application showing the data.

Do I need to say again that I am a beginner.

<CODE>
Frm_Main_Load(object sender, EventArgs e)
OleDbConnection DBConnection = new OleDbConnection();
OleDbDataAdapter DataAdapter_QC; //1
DataTable LocalDataTableALL = new DataTable();
//Set path of DB file.
DBConnection.ConnectionString = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0.;Data Source=|DataDirectory|\QC-Base_Method_Database_BE.accdb;User id=Admin;Persist Security Info=True;Jet OLEDB:Database Password=mydatabasepassword");
//Open connection to DB.
DBConnection.Open();
//Tell data adapter what data to get from which table.
DataAdapter_ALL = new OleDbDataAdapter("SELECT * FROM tbl_Methods WHERE (Approved = true) AND (Archived = false) ORDER BY MethodNumber", DBConnection);
//Load everything from Access to a local data table using the adapter.
DataAdapter_ALL.Fill(LocalDataTableALL);
DBConnection.Close();
//CODE THAT TIES THE DATA TO A BINDING NAVIGATOR !
BindingSource bindingsrcALL = new BindingSource();
bindingsrcALL.DataSource = LocalDataTableALL; //<---------
this.dataGridViewALL.DataSource = bindingsrcALL;
bindingNavigator_ALLMethods.BindingSource = bindingsrcALL;
this.dataGridViewALL.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;

//BELOW IS WHERE I GET A NULL REFERENCE EXCEPTION.
this.dataGridViewALL.Columns["Method_Title"].Width = 100;
</CODE>
 
That means that you have not instantiated the dataGridViewALL yet and you are trying to use it. Based on your (vague) description, it sounds like that should be part of the tab when it is created. If that is really the case, then form load is not the right place since you have to wait until after the tabs are created.
 
Frm_Main() is called from a Frm_Login upon successful login. Frm_Main calls InitializeComponent() which is what VS Designer generated. It is in the Frm_Main Designer generated code that I find
C#:
 this.dataGridViewALL = new System.Windows.Forms.DataGridView()
. All of the InitializeComponent code (which Microsoft warns not to modify the contents with code editor) leads me to believe that dataGridViewALL was instantiated before I ever get to Frm_Main_Load event. The InitializeComponent of course is part of the same public partial class of Frm_Main, so I expected that is why the datagridviews all populate and work when Frm_Main loads. If indeed all of the instantiation is taking place in the Frm_Main.Designer.cs -- on which particular event would I best place the code to modify the datagridview column properties?
 
Back
Top Bottom