DataGridView and # in column names

0
0

Hi,

we have some tables (sql server) that have # in column names like #Person. All queries work without issues.

but when binding source based on table with fileds containing # is assigned to DataGridView (in designer as data source) error is reported and grid is not bound to it.

I know that columns can be renamed, but in somecases it is not easy to accomodate it, any ideas how to fix this issue?

thanx,

D

  • You must to post comments
0
0

Hi Dino,

The error gets thrown specifically because of the “#” character that’s present in your tables, because when you automatically bind the DataGridView, the dataset would try to prepend “col” to the column name and it’s an invalid character for an identifier.

The workaround would be to ultimately map the tables manually to a new binding source, or to just rename the table columns.
I guess setting everything manually won’t be an issue since the queries work fine.

Here’s a small example of what you can potentially do :

SqlConnection cn = new SqlConnection("\\your_connection_string");
SqlDataAdapter cmd = new SqlDataAdapter();
DataTable dt = new DataTable("your_test_table");
cmd.SelectCommand = new SqlCommand();
cmd.SelectCommand.Connection = cn;
cmd.SelectCommand.CommandText = "your_select_query";
cmd.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();

Best regards,
Alaa
  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.