470,596 Members | 1,370 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,596 developers. It's quick & easy.

Hide datagrid column

Das
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/de...ingcolumns.asp
Thanks in advance...

Regards,

das

Nov 17 '05 #1
6 8137
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" <Da*@discussions.microsoft.com> wrote in message
news:A6**********************************@microsof t.com...
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/de...ingcolumns.asp
Thanks in advance...

Regards,

das


Nov 17 '05 #2
Das
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 [MVP]" wrote:
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" <Da*@discussions.microsoft.com> wrote in message
news:A6**********************************@microsof t.com...
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/de...ingcolumns.asp
Thanks in advance...

Regards,

das


Nov 17 '05 #3
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.

Nov 17 '05 #4
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.

Nov 17 '05 #5
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" <Da*@discussions.microsoft.com> wrote in message
news:22**********************************@microsof t.com...
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 [MVP]" wrote:
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" <Da*@discussions.microsoft.com> wrote in message
news:A6**********************************@microsof t.com...
> 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/de...ingcolumns.asp
>
>
> Thanks in advance...
>
> Regards,
>
> das
>



Nov 17 '05 #6
Das
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:
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.

Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by CBuck | last post: by
1 post views Thread by enak | last post: by
3 posts views Thread by sivaraman.S | last post: by
5 posts views Thread by J | last post: by
1 post views Thread by nuhura01 | last post: by
1 post views Thread by TonyJ | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.