By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,890 Members | 1,226 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,890 IT Pros & Developers. It's quick & easy.

Deleting a Datagrid column

P: n/a
Hello,

Please pardon my ignorance as I'm sure this is easy to do. I have a datagrid
where I want to let the user delete columns. I added a context menu to the
datagrid that has a delete option. If the user right clicks on a column
heading can I highlight that column and then delete it? I'm not exactly sure
how to highlight it or figure out what column has been clicked.

In my testing so far when I use the context menu I always get the error
"Column 'ColumnName' does belong to table" - I'm trying to remove the first
column:

((DataTable)dgdData.DataSource).Columns.RemoveAt(0 );

When I added a button and use the same line of code I do not get the error
and the column is removed. Any ideas why?

Thanks for any help, I really appreciate it.

Thanks,
Nick
Jan 4 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
I don't think you should delete columns.
instead, use a view that shows colums of a data table. then, you can simply
change the colums that are shown.
that way te columns stay where they are, but they are just not shown.

kind regards,
Bruno.
"Nick" <ni********@nospam.nospam> wrote in message
news:46**********************************@microsof t.com...
Hello,

Please pardon my ignorance as I'm sure this is easy to do. I have a
datagrid
where I want to let the user delete columns. I added a context menu to the
datagrid that has a delete option. If the user right clicks on a column
heading can I highlight that column and then delete it? I'm not exactly
sure
how to highlight it or figure out what column has been clicked.

In my testing so far when I use the context menu I always get the error
"Column 'ColumnName' does belong to table" - I'm trying to remove the
first
column:

((DataTable)dgdData.DataSource).Columns.RemoveAt(0 );

When I added a button and use the same line of code I do not get the error
and the column is removed. Any ideas why?

Thanks for any help, I really appreciate it.

Thanks,
Nick

Jan 4 '06 #2

P: n/a
Bruno,

Thanks for your help. I'm actually not loading database records into the
datagrid. They are values I'm pulling in from a text file. I want to allow
them to do whatever they want to the data and then I will save it back to the
file.

Thanks,
Nick

"Bruno van Dooren" wrote:
I don't think you should delete columns.
instead, use a view that shows colums of a data table. then, you can simply
change the colums that are shown.
that way te columns stay where they are, but they are just not shown.

kind regards,
Bruno.
"Nick" <ni********@nospam.nospam> wrote in message
news:46**********************************@microsof t.com...
Hello,

Please pardon my ignorance as I'm sure this is easy to do. I have a
datagrid
where I want to let the user delete columns. I added a context menu to the
datagrid that has a delete option. If the user right clicks on a column
heading can I highlight that column and then delete it? I'm not exactly
sure
how to highlight it or figure out what column has been clicked.

In my testing so far when I use the context menu I always get the error
"Column 'ColumnName' does belong to table" - I'm trying to remove the
first
column:

((DataTable)dgdData.DataSource).Columns.RemoveAt(0 );

When I added a button and use the same line of code I do not get the error
and the column is removed. Any ideas why?

Thanks for any help, I really appreciate it.

Thanks,
Nick


Jan 4 '06 #3

P: n/a
regardless, you can load the data from the text file into a data table and
then do what i suggested.
you can fill data tables from all sorts of data sources.

kind regards,
Bruno.
"Nick" <ni********@nospam.nospam> wrote in message
news:46**********************************@microsof t.com...
Bruno,

Thanks for your help. I'm actually not loading database records into the
datagrid. They are values I'm pulling in from a text file. I want to allow
them to do whatever they want to the data and then I will save it back to
the
file.

Thanks,
Nick

"Bruno van Dooren" wrote:
I don't think you should delete columns.
instead, use a view that shows colums of a data table. then, you can
simply
change the colums that are shown.
that way te columns stay where they are, but they are just not shown.

kind regards,
Bruno.
"Nick" <ni********@nospam.nospam> wrote in message
news:46**********************************@microsof t.com...
> Hello,
>
> Please pardon my ignorance as I'm sure this is easy to do. I have a
> datagrid
> where I want to let the user delete columns. I added a context menu to
> the
> datagrid that has a delete option. If the user right clicks on a column
> heading can I highlight that column and then delete it? I'm not exactly
> sure
> how to highlight it or figure out what column has been clicked.
>
> In my testing so far when I use the context menu I always get the error
> "Column 'ColumnName' does belong to table" - I'm trying to remove the
> first
> column:
>
> ((DataTable)dgdData.DataSource).Columns.RemoveAt(0 );
>
> When I added a button and use the same line of code I do not get the
> error
> and the column is removed. Any ideas why?
>
> Thanks for any help, I really appreciate it.
>
> Thanks,
> Nick


Jan 4 '06 #4

P: n/a
Hi Nick,

Thanks for your post.

To highlight the certain column in DataGrid, there is no build-in support,
we have to inherit from the DataGrid control then draw the column
selection. The link below shows the 2 ways of doing this:
"5.59 How can I enable column selections in my datagrid?"
http://64.78.52.104/FAQ/WinForms/FAQ_c44c.asp#q893q

To determine which column the right mouse clicked, we can handle the
MouseDown/MouseUp event, in this event, we can get the mouse position, then
we can use DataGrid.HitTest method to convert the mouse position
information into DataGrid.HitTestInfo instance. Then we can determine the
column index, sample code snippet like this:

private void Form1_Load(object sender, System.EventArgs e)
{
DataTable dt=new DataTable();
dt.Columns.Add(new DataColumn("column1", typeof(int)));
dt.Columns.Add(new DataColumn("column2", typeof(string)));

for(int i=0;i<5;i++)
{
DataRow dr=dt.NewRow();
dr["column1"]=i;
dr["column2"]="item"+i.ToString();
dt.Rows.Add(dr);
}

this.dataGrid1.DataSource=dt;
}

private void dataGrid1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
DataGrid.HitTestInfo hti=this.dataGrid1.HitTest(e.X, e.Y);
MessageBox.Show("column: "+hti.Column.ToString() +" Row:
"+hti.Row.ToString());
}

For the last issue of deleting the column, I am not sure what is the logic
you what. Do you want to delete the DataColumn data in the DataTable, or
you only want to hide the column in the DataGrid UI without the DataColumn
data in the DataTable to be deleted?

Based on your code snippet, it seems that you want the first option.
Currently, your code snippet looks good without much problem. Can you show
me a little sample project to reproduce our your problem? You may attach
the sample project in a further reply.

Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Jan 5 '06 #5

P: n/a
Jeffrey,

Thank you very much for your help. I'm not sure what I was doing wrong, but
I've got it working now. Thanks again.

Nick

""Jeffrey Tan[MSFT]"" wrote:
Hi Nick,

Thanks for your post.

To highlight the certain column in DataGrid, there is no build-in support,
we have to inherit from the DataGrid control then draw the column
selection. The link below shows the 2 ways of doing this:
"5.59 How can I enable column selections in my datagrid?"
http://64.78.52.104/FAQ/WinForms/FAQ_c44c.asp#q893q

To determine which column the right mouse clicked, we can handle the
MouseDown/MouseUp event, in this event, we can get the mouse position, then
we can use DataGrid.HitTest method to convert the mouse position
information into DataGrid.HitTestInfo instance. Then we can determine the
column index, sample code snippet like this:

private void Form1_Load(object sender, System.EventArgs e)
{
DataTable dt=new DataTable();
dt.Columns.Add(new DataColumn("column1", typeof(int)));
dt.Columns.Add(new DataColumn("column2", typeof(string)));

for(int i=0;i<5;i++)
{
DataRow dr=dt.NewRow();
dr["column1"]=i;
dr["column2"]="item"+i.ToString();
dt.Rows.Add(dr);
}

this.dataGrid1.DataSource=dt;
}

private void dataGrid1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
DataGrid.HitTestInfo hti=this.dataGrid1.HitTest(e.X, e.Y);
MessageBox.Show("column: "+hti.Column.ToString() +" Row:
"+hti.Row.ToString());
}

For the last issue of deleting the column, I am not sure what is the logic
you what. Do you want to delete the DataColumn data in the DataTable, or
you only want to hide the column in the DataGrid UI without the DataColumn
data in the DataTable to be deleted?

Based on your code snippet, it seems that you want the first option.
Currently, your code snippet looks good without much problem. Can you show
me a little sample project to reproduce our your problem? You may attach
the sample project in a further reply.

Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Jan 5 '06 #6

P: n/a
Jeffrey,

Could I ask one more question? Is there a way to insert a column somewhere
other than the end? I put something together inserts a column, but I lose all
my data.

Thanks,
Nick

""Jeffrey Tan[MSFT]"" wrote:
Hi Nick,

Thanks for your post.

To highlight the certain column in DataGrid, there is no build-in support,
we have to inherit from the DataGrid control then draw the column
selection. The link below shows the 2 ways of doing this:
"5.59 How can I enable column selections in my datagrid?"
http://64.78.52.104/FAQ/WinForms/FAQ_c44c.asp#q893q

To determine which column the right mouse clicked, we can handle the
MouseDown/MouseUp event, in this event, we can get the mouse position, then
we can use DataGrid.HitTest method to convert the mouse position
information into DataGrid.HitTestInfo instance. Then we can determine the
column index, sample code snippet like this:

private void Form1_Load(object sender, System.EventArgs e)
{
DataTable dt=new DataTable();
dt.Columns.Add(new DataColumn("column1", typeof(int)));
dt.Columns.Add(new DataColumn("column2", typeof(string)));

for(int i=0;i<5;i++)
{
DataRow dr=dt.NewRow();
dr["column1"]=i;
dr["column2"]="item"+i.ToString();
dt.Rows.Add(dr);
}

this.dataGrid1.DataSource=dt;
}

private void dataGrid1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
DataGrid.HitTestInfo hti=this.dataGrid1.HitTest(e.X, e.Y);
MessageBox.Show("column: "+hti.Column.ToString() +" Row:
"+hti.Row.ToString());
}

For the last issue of deleting the column, I am not sure what is the logic
you what. Do you want to delete the DataColumn data in the DataTable, or
you only want to hide the column in the DataGrid UI without the DataColumn
data in the DataTable to be deleted?

Based on your code snippet, it seems that you want the first option.
Currently, your code snippet looks good without much problem. Can you show
me a little sample project to reproduce our your problem? You may attach
the sample project in a further reply.

Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Jan 5 '06 #7

P: n/a
Hi Nick,

I am glad your original problem is resolved.

For your further question, I am not sure I understand it very well. For
"insert a column", do you mean insert into the DataTable or DataGrid UI? Do
you use the autogenerated columns by databinding in DataGrid or you
explicitly add the DataGridColumnStyles to DataGrid?

Normally, we'd better not use the autogenerated columns by databinding in
DataGrid, we can explicitly add DataGridColumnStyles to DataGrid, which
DataGridColumnStyle.MappingName can be set to corresponding
DataColumn.ColumnName. If we do this, after inserting a new column into the
DataTable(with data), we can also programmatically insert a
DataGridColumnStyle to the position you want in
DataGrid.TableStyles[0].GridColumnStyles property, with setting this
GridColumnStyle.MappingName to the new inserted columnname, it will display
in this new GridColumnStyle without any problem. So we can display the new
inserted column in DataGrid any order we want.

Hope this helps

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Jan 6 '06 #8

P: n/a
Hi Jeffrey,

Thanks again for your help. I have never really used DataGridColumnStyles.
For testing purposes I have this:

DataTable myDataTable = new DataTable();
DataColumn myDataColumn;
for(int z=0;z<5; z++)
{
myDataTable.Columns.Add(new DataColumn(""));
}

dataGrid1.DataSource=myDataTable;
Could I use DataGridColumnStyles with this sample?

Thanks,
Nick

""Jeffrey Tan[MSFT]"" wrote:
Hi Nick,

I am glad your original problem is resolved.

For your further question, I am not sure I understand it very well. For
"insert a column", do you mean insert into the DataTable or DataGrid UI? Do
you use the autogenerated columns by databinding in DataGrid or you
explicitly add the DataGridColumnStyles to DataGrid?

Normally, we'd better not use the autogenerated columns by databinding in
DataGrid, we can explicitly add DataGridColumnStyles to DataGrid, which
DataGridColumnStyle.MappingName can be set to corresponding
DataColumn.ColumnName. If we do this, after inserting a new column into the
DataTable(with data), we can also programmatically insert a
DataGridColumnStyle to the position you want in
DataGrid.TableStyles[0].GridColumnStyles property, with setting this
GridColumnStyle.MappingName to the new inserted columnname, it will display
in this new GridColumnStyle without any problem. So we can display the new
inserted column in DataGrid any order we want.

Hope this helps

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Jan 9 '06 #9

P: n/a
Hi Nick,

Thanks for your feedback.

Yes, for product level DataGrid usage, we should always use explicit
DataGridColumnStyles for the DataSource, then we can control the column in
details. Normally, we should first create a DataGridTableStyle and set its
MappingName to DataTable.TableName, then create several
DataGridColumnStyles, each for one DataColumn, then set each
DataGridColumnStyle.MappingName to corresponding DataColumn.ColumnName. At
last, we should add these DataGridColumnStyles into DataGridTableStyle
..GridColumnStyles property, and add this DataGridTableStyle into
DataGrid.TableStyles property.

Note: the most important thing is we must set the MappingName property
correctly, or the DataGridTableStyle and DataGridColumnStyles may be
invisible.

Below article provided a detailed information regarding DataGrid
DataGridColumnStyle:
"Styling with the DataGridColumnStyle, Part 1"
http://msdn.microsoft.com/library/de...us/dnwinforms/
html/datagridcolumnstyle1.asp

If you want to know how to move columns in DataGrid, please refer to the
below FAQ:
"5.19 How do I move columns in a datagrid?"
http://64.78.52.104/FAQ/WinForms/FAQ_c44c.asp#q764q

Hope this helps

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Jan 10 '06 #10

P: n/a
Hi Nick,

Does my reply make sense to you? Is your problem resolved? Please feel free
to tell me, I will follow up with you. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Jan 12 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.