locked
How can i hide or delete a column of datagrid RRS feed

  • Question

  • Hi friends
    i wanna show specific Column of my database  in data grid

    i use the below code to hide a column but it doesn't work
    now how i can hide or delete a column of data grid???

    DataGridTableStyle ts = new DataGridTableStyle();
                DS.Tables[0].TableName = "Records";
                ts.MappingName = "Records";

                DataGridTextBoxColumn Column = new DataGridTextBoxColumn();

                Column.MappingName = "CoName";
                ts.GridColumnStyles.Add(Column);

                ts.GridColumnStyles[0].Width = 0;

                dtgResult.DataSource = DS.Tables[0];
    Saturday, September 5, 2009 10:12 AM

Answers

  • Hi,

    The project Hide DataGrid Column Tester is an example of how to hide the first column of three of a DataGrid, the left softkey populates the data, the right softkey applies the appropriate table style :-

    http://www.smartmobiledevice.co.uk/Projects

    Hope this helps.

    Paul Diston
    http://www.smartmobiledevice.co.uk/
    • Marked as answer by pouya .samie Saturday, September 5, 2009 12:03 PM
    Saturday, September 5, 2009 10:56 AM
  • Hi pouya,

    Please refer below code snippet:

     
            private void dgStyle()
            {
                try
                {
    
                    DataGridTableStyle myGridTableStyle = new DataGridTableStyle();
                    DataGridTextBoxColumn myColumn = new DataGridTextBoxColumn();
                    myGridTableStyle.MappingName = "Testing";
    
                    myColumn.Width = 0;
                    myColumn.MappingName = "ID";
                    myColumn.HeaderText = "ID";
                    myGridTableStyle.GridColumnStyles.Add(myColumn);
    
                    DataGridTextBoxColumn myColumn1 = new DataGridTextBoxColumn();
                    myColumn1.Width = 50;
                    myColumn1.MappingName = "Name";
                    myColumn1.HeaderText = "Name";
                    myGridTableStyle.GridColumnStyles.Add(myColumn1);
    
                    DataGridTextBoxColumn myColumn2 = new DataGridTextBoxColumn();
                    myColumn2.Width = 50;
                    myColumn2.MappingName = "Location";
                    myColumn2.HeaderText = "Location";
                    myGridTableStyle.GridColumnStyles.Add(myColumn2);
    
                  
                    dataGrid1.TableStyles.Add(myGridTableStyle);
                    dataGrid1.DataSource = BuildDataTable();
    
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
    
    
            private DataTable BuildDataTable()
            {
                try
                {
                    DataTable table = new DataTable("Testing");
                    table.Columns.Add("ID", typeof(int));
                    table.Columns.Add("Name", typeof(string));
                    table.Columns.Add("Location", typeof(string));
                    table.Rows.Add("57", "Pavan", "Kota");
                    table.Rows.Add("57", "Kamal", "Bikaner");
                    table.Rows.Add("57", "Nikhil", "Ajmer");
    
                    return table;
                }
                catch
                {
                    throw;
                }
            }


    Hope this may be help.

    If you find my post is helpful for you then mark as a answer.
    • Marked as answer by pouya .samie Saturday, September 5, 2009 12:04 PM
    Saturday, September 5, 2009 11:38 AM

All replies

  • Hi,

    Have you tried setting the Width of Column (Column.Width) before adding it to the GridColumnStyles collection?

    Hope this helps.

    Paul Diston
    http://www.smartmobiledevice.co.uk/
    Saturday, September 5, 2009 10:32 AM
  • hi I use System.Windows.Forms.DataGrid the column.Width property is in System.Windows.Forms.DataGridView Object But this control dose not work on windows mobile
    Saturday, September 5, 2009 10:38 AM
  • Hi,

    The project Hide DataGrid Column Tester is an example of how to hide the first column of three of a DataGrid, the left softkey populates the data, the right softkey applies the appropriate table style :-

    http://www.smartmobiledevice.co.uk/Projects

    Hope this helps.

    Paul Diston
    http://www.smartmobiledevice.co.uk/
    • Marked as answer by pouya .samie Saturday, September 5, 2009 12:03 PM
    Saturday, September 5, 2009 10:56 AM
  • Hi pouya,

    Please refer below code snippet:

     
            private void dgStyle()
            {
                try
                {
    
                    DataGridTableStyle myGridTableStyle = new DataGridTableStyle();
                    DataGridTextBoxColumn myColumn = new DataGridTextBoxColumn();
                    myGridTableStyle.MappingName = "Testing";
    
                    myColumn.Width = 0;
                    myColumn.MappingName = "ID";
                    myColumn.HeaderText = "ID";
                    myGridTableStyle.GridColumnStyles.Add(myColumn);
    
                    DataGridTextBoxColumn myColumn1 = new DataGridTextBoxColumn();
                    myColumn1.Width = 50;
                    myColumn1.MappingName = "Name";
                    myColumn1.HeaderText = "Name";
                    myGridTableStyle.GridColumnStyles.Add(myColumn1);
    
                    DataGridTextBoxColumn myColumn2 = new DataGridTextBoxColumn();
                    myColumn2.Width = 50;
                    myColumn2.MappingName = "Location";
                    myColumn2.HeaderText = "Location";
                    myGridTableStyle.GridColumnStyles.Add(myColumn2);
    
                  
                    dataGrid1.TableStyles.Add(myGridTableStyle);
                    dataGrid1.DataSource = BuildDataTable();
    
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
    
    
            private DataTable BuildDataTable()
            {
                try
                {
                    DataTable table = new DataTable("Testing");
                    table.Columns.Add("ID", typeof(int));
                    table.Columns.Add("Name", typeof(string));
                    table.Columns.Add("Location", typeof(string));
                    table.Rows.Add("57", "Pavan", "Kota");
                    table.Rows.Add("57", "Kamal", "Bikaner");
                    table.Rows.Add("57", "Nikhil", "Ajmer");
    
                    return table;
                }
                catch
                {
                    throw;
                }
            }


    Hope this may be help.

    If you find my post is helpful for you then mark as a answer.
    • Marked as answer by pouya .samie Saturday, September 5, 2009 12:04 PM
    Saturday, September 5, 2009 11:38 AM
  • thank you paul it has worked ;)
    Saturday, September 5, 2009 12:05 PM
  • Saturday, September 5, 2009 12:06 PM