DGV Datasource, LImiting Columns

JaedenRuiner

Well-known member
Joined
Aug 13, 2007
Messages
340
Programming Experience
10+
Alright,

I have a dataset with a datatable and that table has 5 fields, PKID, FK_ID, index, name, import. If I bind the datasource and datamember fields of the DGV all 5 columns display, however, I want to limit the dgv to only show the index, name, & import fields, and have the Row Headers list text equal to "MyString " & [index] (as in the index field of the current row).

How would I do this?

Thanks
 
You can go into the Collumns Collection for the DataGridView from the DataGridView Tasks menu (the little triangle in the upper right hand corner) or you can go to the DataGridView Properties menu and click on the columns collection.

From there you can make the changes you require.

If you need to do it programmatically you could do something like this:

VB.NET:
        myDataGridView.Columns.Remove("ColumnName")

        myDataGridView.Columns("ColumnName").HeaderText = _
            "Hi Mom " & myDataGridView.Columns("ColumnName").Index.ToString()
 
Hrm.

Well, A) I wanted it to be part of the Row Headers. It's funny that you can set the RowTemplate(Col -1), but you can't access it with a programmatic datasource.

I found that with AutoGenerateColumns = False, you can define the columns and then set them individually to the column name you want. So I've got 3 columns:
  • dgv_colIndex.DataPropertyName = "index"
  • dgv_colImport.DataPropertyname = "import"
  • dgv_colMap.DataPropertyName = "dbmap"

Then All i need to is set the datasource and datamember of the DGV and all is well. I was just hoping that I could do something similar for the RowHeaders. (Basically Column(-1) to the DGV, but that column is like off limits or something, grr).

I have in my table a Column who's expression is set to 'Column_' & [index] and that works perfectly, so I thought I should be able to set the RowHeaderColumn.DataPropertyName = "colname" and not have to worry about it again. *shrug*, oh well. I'll figure something else out, maybe a readonly column and hide the rowheaders or something.

Thanks
 
Oops Row Header :-(.

I was able to set the text in the row header by using

VB.NET:
        myDataGridView.Rows(0).HeaderCell.Value = "My Text"
 
Back
Top