Datagrid Datasource

Hi Guys,

Could anyone pls tell me if its possible while binding the data to datasource you only get certain columns from the datatable like 1 - 5 and ignore the last column...


DataGrid1.DataSource = ds.Tables[0];
DataGrid1.DataBind();

Cheers
--Nitin
radhakrishanAsked:
Who is Participating?
 
aprestoCommented:
Otherwise have a look here, shows you how to hide datagrid columns:

http://www.c-sharpcorner.com/Code/2003/June/HideDataGridColDyna.asp
0
 
aprestoCommented:
Hi radhakrishan,

Is the datatable being populated from a database - if so could you not just change your sql?

Apresto
0
 
annihil8Commented:
I would use a tablestyle

DataGridColumnStyle colstyle;
_dg.TableStyles.Clear();

DataGridTableStyle tablestyle = new DataGridTableStyle();
tablestyle .MappingName = "xxxNAME OF TABLE";

//
colstyle = new DataGridTextBoxColumn();
colstyle.MappingName = "xxxNAME OF COLUMNxxx";
colstyle.HeaderText = "Free to choose";
colstyle.Width = 100;
tablestyle .GridColumnStyles.Add(colstyle);
                  
//Do this for every col you want to show
                  
_dg.TableStyles.Add(tablestyle );

Grtz,
Annihil8
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
radhakrishanAuthor Commented:
Hi Annihil8,

DataGrid1.TableStyles.Clear();
                        DataGridTableStyle tablestyle = new DataGridTableStyle();
                        tablestyle.MappingName = "tbCategories";
                        colstyle = new DataGridTextBoxColumn();
                        colstyle.MappingName = "Start Date";
                        colstyle.HeaderText = "Free to choose";
                        colstyle.Width = 100;
                        tablestyle .GridColumnStyles.Add(colstyle);
                        DataGrid1.TableStyles.Add(tablestyle);

Doing this gives me loads of errors:

--System.Web.UI.WebControls.DataGrid' does not contain a definition for 'TableStyles'
--The name 'colstyle' does not exist in the class or namespace 'CCalendar.Daydescription'

And how do I tell it to take the data from dataset
do I still need to say:
datagrid1.datasource = ds.tables[o]

0
 
radhakrishanAuthor Commented:
Hi apresto ,
The link you provided gave me exactly wat i was lookin for.
Cheers.
0
 
annihil8Commented:
Allright :D

My code is for winforms and even on CF. I'm not familiar at all with webdevelopment so I can't say where the error is.

-Colstyle need to be declared as :
DataGridColumnStyle colstyle; // but you will get the error that DataGridColumnStyle is unknown
-you still have to do datagrid1.datasource = ds.tables[o] after the
DataGrid1.TableStyles.Add(tablestyle);

Following msdn it should work, but I got the same errors like you when testing it on webform :D Probably a reference or something, but I can't tell it exactly.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.