0
votes

I have a datagridview in my winforms c# app in .net 3.5 and visual studio 2008.

From code, in the form constructor, and after InitializeComponent() method I set the datagridview's datasource as below:

this.dtItems = new DataTable();
this.dgv.DataSource = this.dtItems;

dtItems.Columns.Add(String.Empty, typeof(byte[]));  // This will contain a png image, and I do not want to put a header text so string.empty used but it does not work
dtItems.Columns.Add("Date", typeof(DateTime));
dtItems.Columns.Add("ID", typeof(string));
dtItems.Columns.Add("Name", typeof(string));
dtItems.Columns.Add("Department", typeof(string));

dtItems is a private global variable of DataTable type:

private DataTable dtItems = null;

Problems I have:

  1. I would like to add first column with no header text but it is not working.
  2. When running, empty datatable is associated correctly to datagridview's datasource but one extra column is added at the leftmost. Why?

Updated:

Second point solved by using below line of code:

dgv.RowHeadersVisible = false;
1
By not working what do you mean, it doesn't display column or something else?Aleksa Ristic
@AleksaRistic It displays the column but with column name set to "column1" automatically despite of I have indicated string.empty for header text.Ralph
To remove first column use RowHeadersVisible and set it to false. For second one i do not know how you even get any output since you are asigning datasource for dgv before you even set properties for dtItems. Try moving this.dgv.DataSource after all Columns.AddAleksa Ristic
@AleksaRistic it does the same. What I want is to create the first column with no header text, I mean, blank header text. How can I do this?Ralph

1 Answers

1
votes

To remove first column use RowHeadersVisible and set it to false.

To make column without text simply add " " (with space) and it will work visually but if you want to detect if String.IsNullOrEmpty() it will say false so you will need to use if String.IsNullOrWhiteSpace()