473,222 Members | 1,756 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,222 software developers and data experts.

Deleting a Datagrid column

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
10 2297
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Junkguy | last post by:
I'm having difficulty deleting rows from a datagrid. I want to put a "delete" button on a form and achieve the same functionality as hitting the "delete" key on the keyboard for the selected row of...
5
by: Mojtaba Faridzad | last post by:
Hi, with SetDataBinding( ) a DataGrid shows a DataView. user can select some rows in the grid by holding cotrol key. when user clicks on Delete button, I should delete all selected rows. I am...
3
by: PeterZ | last post by:
Hi, In a running C# app with a datagrid control I select all rows in the dataGrid using CTRL-A, I then paste into some other app like notepad or Word but the column headings get left off. Is...
0
by: Hrvoje Vrbanc | last post by:
Hello, this is a problem I came upon while building a site based on MCMS 2002 but it's not strictly MCMS-oriented: I have a page that displays a certain content in presentation mode but when an...
2
by: CSL | last post by:
I am using the DataGrid in a Windows Application, how can I adjust the widths of each column individually.
1
by: | last post by:
Hello, I have a datagrid bound to a datatable which has an Identity column. I load the table from data in a spreadsheet and manually assign seq. values to the identity column. Once that is...
2
by: rodchar | last post by:
hey all, i've got an asp.net page with a datagrid on it. when i delete all the rows in the datagrid it just shows the column headers. how are some ways i can clean that up? thanks, rodchar
3
by: marshallarts | last post by:
Hello, I have a datagrid (grdShots) on a form, and a button to allow the user to delete records from the dataset underlying the grid. My code appears to work, because the row disappears from...
2
by: =?Utf-8?B?Y3JlYXZlczA2MjI=?= | last post by:
I have a nested datagrid in a xaml file, the parent datagrid loads the vendor information and the details loads the documents for that vendor in a datagrid. Everything is working fine until I click...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.