Hide datagrid column

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

    Hide datagrid column

    Hi everyone,
    I'm using datagrid control to display the data. I want to hide column to be
    displayed into the data grid. I'm using the code as given below:

    Method given below is used to bind the data grid with the db:

    public static void DataGrid(DataGr id DG,string sql)
    {

    //Initialize connection with the db
    SqlConnection co =initConnection ();

    SqlDataAdapter myCommand = new SqlDataAdapter( sql, co );
    DataSet ds = new DataSet();
    myCommand.Fill( ds);

    DG.DataSource =ds.Tables[0].DefaultView;

    co.Close();
    }



    clsGeneral.Data Grid(dgDestinat ion,"Select strCode as Code, strName as Name,
    acDestination from tblDestination where acDestination != 0");

    // Set the DataGridTableSt yle.MappingName property
    // to the table in the data source to map to.
    ts.MappingName = "tblDestination ";


    // Add it to the datagrid's TableStyles collection

    // Create new DataGridColumnS tyle objects.
    DataGridColumnS tyle acDestination = new DataGridTextBox Column();
    acDestination.M appingName = "acDestination" ;
    //cOrderDate.Head erText = "Order Date";
    acDestination.W idth = 0;
    ts.GridColumnSt yles.Add(acDest ination);
    dgDestination.T ableStyles.Add( ts);
    dgDestination.R efresh();

    I'm using the code as given in link below. I don't know what's wrong with
    the code




    Thanks in advance...

    Regards,

    das

  • Dmytro Lapshyn [MVP]

    #2
    Re: Hide datagrid column

    Hi Das,

    Just don't add a DataGridColumnS tyle for the column you want to hide to the
    GridColumnStyle s collection .

    --
    Sincerely,
    Dmytro Lapshyn [Visual Developer - Visual C# MVP]


    "Das" <Das@discussion s.microsoft.com > wrote in message
    news:A6CF70A4-F480-4BBE-BD9A-AEC083A74D4C@mi crosoft.com...[color=blue]
    > Hi everyone,
    > I'm using datagrid control to display the data. I want to hide column to
    > be
    > displayed into the data grid. I'm using the code as given below:
    >
    > Method given below is used to bind the data grid with the db:
    >
    > public static void DataGrid(DataGr id DG,string sql)
    > {
    >
    > //Initialize connection with the db
    > SqlConnection co =initConnection ();
    >
    > SqlDataAdapter myCommand = new SqlDataAdapter( sql, co );
    > DataSet ds = new DataSet();
    > myCommand.Fill( ds);
    >
    > DG.DataSource =ds.Tables[0].DefaultView;
    >
    > co.Close();
    > }
    >
    >
    >
    > clsGeneral.Data Grid(dgDestinat ion,"Select strCode as Code, strName as
    > Name,
    > acDestination from tblDestination where acDestination != 0");
    >
    > // Set the DataGridTableSt yle.MappingName property
    > // to the table in the data source to map to.
    > ts.MappingName = "tblDestination ";
    >
    >
    > // Add it to the datagrid's TableStyles collection
    >
    > // Create new DataGridColumnS tyle objects.
    > DataGridColumnS tyle acDestination = new DataGridTextBox Column();
    > acDestination.M appingName = "acDestination" ;
    > //cOrderDate.Head erText = "Order Date";
    > acDestination.W idth = 0;
    > ts.GridColumnSt yles.Add(acDest ination);
    > dgDestination.T ableStyles.Add( ts);
    > dgDestination.R efresh();
    >
    > I'm using the code as given in link below. I don't know what's wrong with
    > the code
    >
    > http://msdn.microsoft.com/library/de...ingcolumns.asp
    >
    >
    > Thanks in advance...
    >
    > Regards,
    >
    > das
    >[/color]

    Comment

    • Das

      #3
      Re: Hide datagrid column

      Hi there,
      thanks for the reply,

      I have done that as well. I added the DataGridColumnS tyle for all other
      column. I mean what I did that I added the DataGridColumnS tyle for other
      columns as given below:

      DataGridColumnS tyle strCode = new DataGridTextBox Column();
      strCode.Mapping Name = "strCode";
      ts.GridColumnSt yles.Add(strCod e);


      DataGridColumnS tyle strName = new DataGridTextBox Column();
      strName.Mapping Name = "strName";
      ts.GridColumnSt yles.Add(strNam e);

      dgDestination.T ableStyles.Add( ts);
      dgDestination.R efresh();

      I'm defining the mappingName with the field I think this is the way you want
      me to set the column style for the rest of the columns. But it doean't seems
      working. I don't know what's wrong with code.

      But there should be some other way because I only want to hide one column.
      I simply want to width of only one column. Rest should be ok.

      Regards,

      das

      "Dmytro Lapshyn [MVP]" wrote:
      [color=blue]
      > Hi Das,
      >
      > Just don't add a DataGridColumnS tyle for the column you want to hide to the
      > GridColumnStyle s collection .
      >
      > --
      > Sincerely,
      > Dmytro Lapshyn [Visual Developer - Visual C# MVP]
      >
      >
      > "Das" <Das@discussion s.microsoft.com > wrote in message
      > news:A6CF70A4-F480-4BBE-BD9A-AEC083A74D4C@mi crosoft.com...[color=green]
      > > Hi everyone,
      > > I'm using datagrid control to display the data. I want to hide column to
      > > be
      > > displayed into the data grid. I'm using the code as given below:
      > >
      > > Method given below is used to bind the data grid with the db:
      > >
      > > public static void DataGrid(DataGr id DG,string sql)
      > > {
      > >
      > > //Initialize connection with the db
      > > SqlConnection co =initConnection ();
      > >
      > > SqlDataAdapter myCommand = new SqlDataAdapter( sql, co );
      > > DataSet ds = new DataSet();
      > > myCommand.Fill( ds);
      > >
      > > DG.DataSource =ds.Tables[0].DefaultView;
      > >
      > > co.Close();
      > > }
      > >
      > >
      > >
      > > clsGeneral.Data Grid(dgDestinat ion,"Select strCode as Code, strName as
      > > Name,
      > > acDestination from tblDestination where acDestination != 0");
      > >
      > > // Set the DataGridTableSt yle.MappingName property
      > > // to the table in the data source to map to.
      > > ts.MappingName = "tblDestination ";
      > >
      > >
      > > // Add it to the datagrid's TableStyles collection
      > >
      > > // Create new DataGridColumnS tyle objects.
      > > DataGridColumnS tyle acDestination = new DataGridTextBox Column();
      > > acDestination.M appingName = "acDestination" ;
      > > //cOrderDate.Head erText = "Order Date";
      > > acDestination.W idth = 0;
      > > ts.GridColumnSt yles.Add(acDest ination);
      > > dgDestination.T ableStyles.Add( ts);
      > > dgDestination.R efresh();
      > >
      > > I'm using the code as given in link below. I don't know what's wrong with
      > > the code
      > >
      > > http://msdn.microsoft.com/library/de...ingcolumns.asp
      > >
      > >
      > > Thanks in advance...
      > >
      > > Regards,
      > >
      > > das
      > >[/color]
      >
      >[/color]

      Comment

      • Philip Hristov

        #4
        Re: Hide datagrid column

        Das,

        What exactly happens? The Column which you try to hide is still
        present? Here I made some tests and I was successfuly able to hide the
        column, also instead of "width" it to 0 try to set the MapingName to
        empty string. Here the both methods works.

        Regards,

        Philip.

        Comment

        • Philip Hristov

          #5
          Re: Hide datagrid column

          Das,

          Why do not try to "zero width" the desired column after adding it to
          the DataGridColumnS tyle collection? Something like this:
          DataGrid1.Table Styles(0).GridC olumnStyles(0). Width = 0

          It should work!

          @Dmytro
          I believe he wants to hide the column, not just not to show it. When
          given column is hidden, it can be easily shown later, without creating
          new one and adding it to collection.

          Best wishes to both of you,

          Philip.

          Comment

          • Dmytro Lapshyn [MVP]

            #6
            Re: Hide datagrid column

            Das,

            Make sure you define the table and column styles BEFORE you bind the grid to
            the data source. Also, I'd recommend that you use the SetDataBinding method
            to specify the data source and the data member (as opposed to setting the
            corresponding properties directly).

            --
            Sincerely,
            Dmytro Lapshyn [Visual Developer - Visual C# MVP]


            "Das" <Das@discussion s.microsoft.com > wrote in message
            news:2257F425-96E4-456E-A06D-5F575E08690C@mi crosoft.com...[color=blue]
            > Hi there,
            > thanks for the reply,
            >
            > I have done that as well. I added the DataGridColumnS tyle for all other
            > column. I mean what I did that I added the DataGridColumnS tyle for other
            > columns as given below:
            >
            > DataGridColumnS tyle strCode = new DataGridTextBox Column();
            > strCode.Mapping Name = "strCode";
            > ts.GridColumnSt yles.Add(strCod e);
            >
            >
            > DataGridColumnS tyle strName = new DataGridTextBox Column();
            > strName.Mapping Name = "strName";
            > ts.GridColumnSt yles.Add(strNam e);
            >
            > dgDestination.T ableStyles.Add( ts);
            > dgDestination.R efresh();
            >
            > I'm defining the mappingName with the field I think this is the way you
            > want
            > me to set the column style for the rest of the columns. But it doean't
            > seems
            > working. I don't know what's wrong with code.
            >
            > But there should be some other way because I only want to hide one column.
            > I simply want to width of only one column. Rest should be ok.
            >
            > Regards,
            >
            > das
            >
            > "Dmytro Lapshyn [MVP]" wrote:
            >[color=green]
            >> Hi Das,
            >>
            >> Just don't add a DataGridColumnS tyle for the column you want to hide to
            >> the
            >> GridColumnStyle s collection .
            >>
            >> --
            >> Sincerely,
            >> Dmytro Lapshyn [Visual Developer - Visual C# MVP]
            >>
            >>
            >> "Das" <Das@discussion s.microsoft.com > wrote in message
            >> news:A6CF70A4-F480-4BBE-BD9A-AEC083A74D4C@mi crosoft.com...[color=darkred]
            >> > Hi everyone,
            >> > I'm using datagrid control to display the data. I want to hide column
            >> > to
            >> > be
            >> > displayed into the data grid. I'm using the code as given below:
            >> >
            >> > Method given below is used to bind the data grid with the db:
            >> >
            >> > public static void DataGrid(DataGr id DG,string sql)
            >> > {
            >> >
            >> > //Initialize connection with the db
            >> > SqlConnection co =initConnection ();
            >> >
            >> > SqlDataAdapter myCommand = new SqlDataAdapter( sql, co );
            >> > DataSet ds = new DataSet();
            >> > myCommand.Fill( ds);
            >> >
            >> > DG.DataSource =ds.Tables[0].DefaultView;
            >> >
            >> > co.Close();
            >> > }
            >> >
            >> >
            >> >
            >> > clsGeneral.Data Grid(dgDestinat ion,"Select strCode as Code, strName as
            >> > Name,
            >> > acDestination from tblDestination where acDestination != 0");
            >> >
            >> > // Set the DataGridTableSt yle.MappingName property
            >> > // to the table in the data source to map to.
            >> > ts.MappingName = "tblDestination ";
            >> >
            >> >
            >> > // Add it to the datagrid's TableStyles collection
            >> >
            >> > // Create new DataGridColumnS tyle objects.
            >> > DataGridColumnS tyle acDestination = new DataGridTextBox Column();
            >> > acDestination.M appingName = "acDestination" ;
            >> > //cOrderDate.Head erText = "Order Date";
            >> > acDestination.W idth = 0;
            >> > ts.GridColumnSt yles.Add(acDest ination);
            >> > dgDestination.T ableStyles.Add( ts);
            >> > dgDestination.R efresh();
            >> >
            >> > I'm using the code as given in link below. I don't know what's wrong
            >> > with
            >> > the code
            >> >
            >> > http://msdn.microsoft.com/library/de...ingcolumns.asp
            >> >
            >> >
            >> > Thanks in advance...
            >> >
            >> > Regards,
            >> >
            >> > das
            >> >[/color]
            >>
            >>[/color][/color]

            Comment

            • Das

              #7
              Re: Hide datagrid column

              hi everyone,
              The way I'm setting the datasource. It by default assings the MappingName
              as "Table". Not the originational database table because I was assigning the
              database query result to the datasource.

              What I did that I defined the mapping name from table stlyes as "Table"
              then it started working.

              Thanks everybody for your support.

              Regards,

              das

              "Philip Hristov" wrote:
              [color=blue]
              > Das,
              >
              > What exactly happens? The Column which you try to hide is still
              > present? Here I made some tests and I was successfuly able to hide the
              > column, also instead of "width" it to 0 try to set the MapingName to
              > empty string. Here the both methods works.
              >
              > Regards,
              >
              > Philip.
              >
              >[/color]

              Comment

              Working...