Willy
Willy

Reputation: 10638

C# Winforms: Associate datatable to datagriview's datasource - Issues

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;

Upvotes: 0

Views: 77

Answers (1)

Aleksa Ristic
Aleksa Ristic

Reputation: 2499

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()

Upvotes: 1

Related Questions