trouble with a datagridview and a column hided

F

finleeds

hi all

I m using the visual studio designer to create 1 Dataset with 1 table
with 3 columns : Id, Name, Birthday.

Once this done, still with the designer, I m creating a datagridview
and choose my dataset as DataSource.

magic : the columns appear in the datagridview.

Still with the designer, I can change the properties of the fist column
"Id" to hide it.

now it s time to write some code :
on the cellClick event I want to get the value of the Id, unfortunaly
it throws an exception cause the column is hided

DataGridViewRow row =
(DataGridViewRow)dataGridView.Rows[e.RowIndex];

if (row != null)
{
try
{
this.selectedRowIndex =
(int)row.Cells["Id"].Value;
}
catch
{
int a = 1;
}
}

Does anyone know how to get the value of a hiding column when the user
clics on a row ?

thx for your help

ben
 
D

Dave Sexton

Hi Ben,

IIRC, your code should work if you really did hide the column.

By removing the column from the list in the designer you're not hiding the
column - you're completely removing it from the collection.

You must set Visible = false instead.
 
F

finleeds

many thanks for your quick answer
but it s what I did :
//
// idDataGridViewTextBoxColumn
//
this.idDataGridViewTextBoxColumn.DataPropertyName = "Id";
this.idDataGridViewTextBoxColumn.HeaderText = "Id";
this.idDataGridViewTextBoxColumn.Name =
"idDataGridViewTextBoxColumn";
this.idDataGridViewTextBoxColumn.Visible = false;

and It still does not work :(

I wrote a very small project to reproduce this problem, I could send it
to you if you want ?

I ll take all advice or ideas :))

ben



Dave Sexton a écrit :
Hi Ben,

IIRC, your code should work if you really did hide the column.

By removing the column from the list in the designer you're not hiding the
column - you're completely removing it from the collection.

You must set Visible = false instead.

--
Dave Sexton

finleeds said:
hi all

I m using the visual studio designer to create 1 Dataset with 1 table
with 3 columns : Id, Name, Birthday.

Once this done, still with the designer, I m creating a datagridview
and choose my dataset as DataSource.

magic : the columns appear in the datagridview.

Still with the designer, I can change the properties of the fist column
"Id" to hide it.

now it s time to write some code :
on the cellClick event I want to get the value of the Id, unfortunaly
it throws an exception cause the column is hided

DataGridViewRow row =
(DataGridViewRow)dataGridView.Rows[e.RowIndex];

if (row != null)
{
try
{
this.selectedRowIndex =
(int)row.Cells["Id"].Value;
}
catch
{
int a = 1;
}
}

Does anyone know how to get the value of a hiding column when the user
clics on a row ?

thx for your help

ben
 
D

Dave Sexton

Hi Ben,

Well, the name of the column isn't "Id". You've named it,
"idDataGridViewTextBoxColumn".

The indexer of the row.Cells collection requires the row's Name, not the value
of the HeaderText property.

this.selectedRowIndex = (int) row.Cells["idDataGridViewTextBoxColumn"].Value;

BTW, you probably shouldn't use try..catch here. It doesn't really make sense
here; especially since you're not catching any specific exception.

--
Dave Sexton

many thanks for your quick answer
but it s what I did :
//
// idDataGridViewTextBoxColumn
//
this.idDataGridViewTextBoxColumn.DataPropertyName = "Id";
this.idDataGridViewTextBoxColumn.HeaderText = "Id";
this.idDataGridViewTextBoxColumn.Name =
"idDataGridViewTextBoxColumn";
this.idDataGridViewTextBoxColumn.Visible = false;

and It still does not work :(

I wrote a very small project to reproduce this problem, I could send it
to you if you want ?

I ll take all advice or ideas :))

ben



Dave Sexton a écrit :
Hi Ben,

IIRC, your code should work if you really did hide the column.

By removing the column from the list in the designer you're not hiding the
column - you're completely removing it from the collection.

You must set Visible = false instead.

--
Dave Sexton

finleeds said:
hi all

I m using the visual studio designer to create 1 Dataset with 1 table
with 3 columns : Id, Name, Birthday.

Once this done, still with the designer, I m creating a datagridview
and choose my dataset as DataSource.

magic : the columns appear in the datagridview.

Still with the designer, I can change the properties of the fist column
"Id" to hide it.

now it s time to write some code :
on the cellClick event I want to get the value of the Id, unfortunaly
it throws an exception cause the column is hided

DataGridViewRow row =
(DataGridViewRow)dataGridView.Rows[e.RowIndex];

if (row != null)
{
try
{
this.selectedRowIndex =
(int)row.Cells["Id"].Value;
}
catch
{
int a = 1;
}
}

Does anyone know how to get the value of a hiding column when the user
clics on a row ?

thx for your help

ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top