Hide datagrid column

G

Guest

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(DataGrid 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.DataGrid(dgDestination,"Select strCode as Code, strName as Name,
acDestination from tblDestination where acDestination != 0");

// Set the DataGridTableStyle.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 DataGridColumnStyle objects.
DataGridColumnStyle acDestination = new DataGridTextBoxColumn();
acDestination.MappingName = "acDestination";
//cOrderDate.HeaderText = "Order Date";
acDestination.Width = 0;
ts.GridColumnStyles.Add(acDestination);
dgDestination.TableStyles.Add(ts);
dgDestination.Refresh();

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/d...n/html/vbtskaddingdeletingorhidingcolumns.asp


Thanks in advance...

Regards,

das
 
D

Dmytro Lapshyn [MVP]

Hi Das,

Just don't add a DataGridColumnStyle for the column you want to hide to the
GridColumnStyles collection .
 
G

Guest

Hi there,
thanks for the reply,

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

DataGridColumnStyle strCode = new DataGridTextBoxColumn();
strCode.MappingName = "strCode";
ts.GridColumnStyles.Add(strCode);


DataGridColumnStyle strName = new DataGridTextBoxColumn();
strName.MappingName = "strName";
ts.GridColumnStyles.Add(strName);

dgDestination.TableStyles.Add(ts);
dgDestination.Refresh();

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 said:
Hi Das,

Just don't add a DataGridColumnStyle for the column you want to hide to the
GridColumnStyles collection .

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


Das said:
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(DataGrid 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.DataGrid(dgDestination,"Select strCode as Code, strName as
Name,
acDestination from tblDestination where acDestination != 0");

// Set the DataGridTableStyle.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 DataGridColumnStyle objects.
DataGridColumnStyle acDestination = new DataGridTextBoxColumn();
acDestination.MappingName = "acDestination";
//cOrderDate.HeaderText = "Order Date";
acDestination.Width = 0;
ts.GridColumnStyles.Add(acDestination);
dgDestination.TableStyles.Add(ts);
dgDestination.Refresh();

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/d...n/html/vbtskaddingdeletingorhidingcolumns.asp


Thanks in advance...

Regards,

das
 
P

Philip Hristov

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.
 
P

Philip Hristov

Das,

Why do not try to "zero width" the desired column after adding it to
the DataGridColumnStyle collection? Something like this:
DataGrid1.TableStyles(0).GridColumnStyles(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.
 
D

Dmytro Lapshyn [MVP]

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 said:
Hi there,
thanks for the reply,

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

DataGridColumnStyle strCode = new DataGridTextBoxColumn();
strCode.MappingName = "strCode";
ts.GridColumnStyles.Add(strCode);


DataGridColumnStyle strName = new DataGridTextBoxColumn();
strName.MappingName = "strName";
ts.GridColumnStyles.Add(strName);

dgDestination.TableStyles.Add(ts);
dgDestination.Refresh();

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 said:
Hi Das,

Just don't add a DataGridColumnStyle for the column you want to hide to
the
GridColumnStyles collection .

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


Das said:
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(DataGrid 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.DataGrid(dgDestination,"Select strCode as Code, strName as
Name,
acDestination from tblDestination where acDestination != 0");

// Set the DataGridTableStyle.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 DataGridColumnStyle objects.
DataGridColumnStyle acDestination = new DataGridTextBoxColumn();
acDestination.MappingName = "acDestination";
//cOrderDate.HeaderText = "Order Date";
acDestination.Width = 0;
ts.GridColumnStyles.Add(acDestination);
dgDestination.TableStyles.Add(ts);
dgDestination.Refresh();

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/d...n/html/vbtskaddingdeletingorhidingcolumns.asp


Thanks in advance...

Regards,

das
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top