473,591 Members | 2,871 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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)dgd Data.DataSource ).Columns.Remov eAt(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 2333
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********@nos pam.nospam> wrote in message
news:46******** *************** ***********@mic rosoft.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)dgd Data.DataSource ).Columns.Remov eAt(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********@nos pam.nospam> wrote in message
news:46******** *************** ***********@mic rosoft.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)dgd Data.DataSource ).Columns.Remov eAt(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********@nos pam.nospam> wrote in message
news:46******** *************** ***********@mic rosoft.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********@nos pam.nospam> wrote in message
news:46******** *************** ***********@mic rosoft.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)dgd Data.DataSource ).Columns.Remov eAt(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.HitTes t method to convert the mouse position
information into DataGrid.HitTes tInfo instance. Then we can determine the
column index, sample code snippet like this:

private void Form1_Load(obje ct sender, System.EventArg s e)
{
DataTable dt=new DataTable();
dt.Columns.Add( new DataColumn("col umn1", typeof(int)));
dt.Columns.Add( new DataColumn("col umn2", typeof(string)) );

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

this.dataGrid1. DataSource=dt;
}

private void dataGrid1_Mouse Down(object sender,
System.Windows. Forms.MouseEven tArgs e)
{
DataGrid.HitTes tInfo hti=this.dataGr id1.HitTest(e.X , e.Y);
MessageBox.Show ("column: "+hti.Column.To String() +" Row:
"+hti.Row.ToStr ing());
}

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.HitTes t method to convert the mouse position
information into DataGrid.HitTes tInfo instance. Then we can determine the
column index, sample code snippet like this:

private void Form1_Load(obje ct sender, System.EventArg s e)
{
DataTable dt=new DataTable();
dt.Columns.Add( new DataColumn("col umn1", typeof(int)));
dt.Columns.Add( new DataColumn("col umn2", typeof(string)) );

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

this.dataGrid1. DataSource=dt;
}

private void dataGrid1_Mouse Down(object sender,
System.Windows. Forms.MouseEven tArgs e)
{
DataGrid.HitTes tInfo hti=this.dataGr id1.HitTest(e.X , e.Y);
MessageBox.Show ("column: "+hti.Column.To String() +" Row:
"+hti.Row.ToStr ing());
}

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.HitTes t method to convert the mouse position
information into DataGrid.HitTes tInfo instance. Then we can determine the
column index, sample code snippet like this:

private void Form1_Load(obje ct sender, System.EventArg s e)
{
DataTable dt=new DataTable();
dt.Columns.Add( new DataColumn("col umn1", typeof(int)));
dt.Columns.Add( new DataColumn("col umn2", typeof(string)) );

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

this.dataGrid1. DataSource=dt;
}

private void dataGrid1_Mouse Down(object sender,
System.Windows. Forms.MouseEven tArgs e)
{
DataGrid.HitTes tInfo hti=this.dataGr id1.HitTest(e.X , e.Y);
MessageBox.Show ("column: "+hti.Column.To String() +" Row:
"+hti.Row.ToStr ing());
}

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 DataGridColumnS tyles to DataGrid?

Normally, we'd better not use the autogenerated columns by databinding in
DataGrid, we can explicitly add DataGridColumnS tyles to DataGrid, which
DataGridColumnS tyle.MappingNam e can be set to corresponding
DataColumn.Colu mnName. If we do this, after inserting a new column into the
DataTable(with data), we can also programmaticall y insert a
DataGridColumnS tyle to the position you want in
DataGrid.TableS tyles[0].GridColumnStyl es 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 DataGridColumnS tyles.
For testing purposes I have this:

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

dataGrid1.DataS ource=myDataTab le;
Could I use DataGridColumnS tyles 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 DataGridColumnS tyles to DataGrid?

Normally, we'd better not use the autogenerated columns by databinding in
DataGrid, we can explicitly add DataGridColumnS tyles to DataGrid, which
DataGridColumnS tyle.MappingNam e can be set to corresponding
DataColumn.Colu mnName. If we do this, after inserting a new column into the
DataTable(with data), we can also programmaticall y insert a
DataGridColumnS tyle to the position you want in
DataGrid.TableS tyles[0].GridColumnStyl es 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
DataGridColumnS tyles for the DataSource, then we can control the column in
details. Normally, we should first create a DataGridTableSt yle and set its
MappingName to DataTable.Table Name, then create several
DataGridColumnS tyles, each for one DataColumn, then set each
DataGridColumnS tyle.MappingNam e to corresponding DataColumn.Colu mnName. At
last, we should add these DataGridColumnS tyles into DataGridTableSt yle
..GridColumnSty les property, and add this DataGridTableSt yle into
DataGrid.TableS tyles property.

Note: the most important thing is we must set the MappingName property
correctly, or the DataGridTableSt yle and DataGridColumnS tyles may be
invisible.

Below article provided a detailed information regarding DataGrid
DataGridColumnS tyle:
"Styling with the DataGridColumnS tyle, Part 1"
http://msdn.microsoft.com/library/de...us/dnwinforms/
html/datagridcolumns tyle1.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4167
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 a datagrid. I really want to do it generically so I have subclassed datagrid and made my own delete row method but I can only get it to work for datasources that are datatables or dataviews. Specifically when the datasource is a DataSet I...
5
14729
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 trying to delete these lines from the dataview like this: for (int i=0; i < dataView.Count; i++) if (dataGrid.IsSelected(i)) dataView.Delete(i);
3
4858
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 there any way of including column headings when copying/pasting from a running datagrid to notepad? At this stage it looks like I have to write my own code but would rather not if someone knows how to do it.
0
1396
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 editor clicks "Switch To Edit Site" in MCMS console on the page, the page displays a different content, an interface that editor use for upload and deleting files on the web server. There are no problems with the upload but there is a problem...
2
6388
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
3069
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 done, I set the AutoIncrement property to true and the AutoIncrementStep property to 1. When I try to delete the last row in the displayed grid, I get the follwoing message: "This row has been removed from a table and does not have any data.
2
1122
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
1739
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 the datagrid. But the delete is not being updated back to the data source, because if I exit the app then start it again, the record reappears in the grid - it has not been deleted. Code for deleting the row is as follows. Column 0 in the grid...
2
6577
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 in the child datagrid and then go to the combobox and choose another vendor. When the new vendor is loaded nothing shows in the datagrid but the itemsource shows the info is there. Know if I click on the child cell and then click back on the...
0
7934
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7870
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8362
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7992
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8225
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6639
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
3850
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3891
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.