nalb4242 0 Newbie Poster

Hello , i am creating new website using asp net and c# .. what i am basically doing is that i have somedata in my database and will let the user show them in report based on what he is gonna choose to display for example i have in my DB (id , name , birthdate and address ) for example i will be doing some sort of filter for user to choose what to display and he chose (id and name ) i will added these coloumns to my gridview and i will create new datatable in my c# code and then bind that datatable to my gridview ...

in my 1stField i have the checked values (which columns the user chose to display)

foreach (ListItem item in lstFields.Items)


grdData.Columns.Add(new BoundField() { HeaderText = item.Text, DataField = item.Value });


what i wanna ask here how can i add the coloumns in my datatable based on what he choose ...

another question if in my datatable i added all the 4 columns in my database and in my gridview there was two columns only
.. and then i binded the datatable to the gridview will that work as i could not know how to add the columns to my table ????

assume in illustartaed photo there is total of attendance in this week so i need to loop over array of people


so what i did i was apple to add in the gridview the columns i need and also i was able to add the headers in my datatable(dt) dt.Columns.Add(new DataColumn(item.Text)); like that ...
but my proplem i don not know which columns were chose as i wanna do it dynamically but whiat i am doing it next made me do it manually

foreach (var x in persons)
                    DataRow dr = dt.NewRow();
                    dr["PersonId"] = x.PersonId.ToString();
                    dr["PersonName"] = x.PersonName;
                    dr["PersonCode"] = x.PersonCode;

i hope you understand my problem ............Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.