Deleting a Datagrid column

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nick

    #1

    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
  • Bruno van Dooren

    #2
    Re: Deleting a Datagrid column

    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" <nickfinity@nos pam.nospam> wrote in message
    news:462C2354-E4F3-4E36-8D70-9057FF94215D@mi crosoft.com...[color=blue]
    > 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[/color]


    Comment

    • Nick

      #3
      Re: Deleting a Datagrid column

      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:
      [color=blue]
      > 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" <nickfinity@nos pam.nospam> wrote in message
      > news:462C2354-E4F3-4E36-8D70-9057FF94215D@mi crosoft.com...[color=green]
      > > 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[/color]
      >
      >
      >[/color]

      Comment

      • Bruno van Dooren

        #4
        Re: Deleting a Datagrid column

        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" <nickfinity@nos pam.nospam> wrote in message
        news:46660AF2-BC7E-430E-A446-B08045D7AF7F@mi crosoft.com...[color=blue]
        > 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:
        >[color=green]
        >> 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" <nickfinity@nos pam.nospam> wrote in message
        >> news:462C2354-E4F3-4E36-8D70-9057FF94215D@mi crosoft.com...[color=darkred]
        >> > 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[/color]
        >>
        >>
        >>[/color][/color]


        Comment

        • Jeffrey Tan[MSFT]

          #5
          RE: Deleting a Datagrid column

          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?"


          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.

          Comment

          • Nick

            #6
            RE: Deleting a Datagrid column

            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:
            [color=blue]
            > 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.
            >
            >[/color]

            Comment

            • Nick

              #7
              RE: Deleting a Datagrid column

              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:
              [color=blue]
              > 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.
              >
              >[/color]

              Comment

              • Jeffrey Tan[MSFT]

                #8
                RE: Deleting a Datagrid column

                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.

                Comment

                • Nick

                  #9
                  RE: Deleting a Datagrid column

                  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:
                  [color=blue]
                  > 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.
                  >
                  >[/color]

                  Comment

                  • Jeffrey Tan[MSFT]

                    #10
                    RE: Deleting a Datagrid column

                    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?"


                    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.

                    Comment

                    • Jeffrey Tan[MSFT]

                      #11
                      RE: Deleting a Datagrid column

                      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.

                      Comment

                      Working...