I'm using a web service that returns a dataset. in this dataset there are 5 table, let's say table A, B, C, D, E. I use table A.
So
DataTable dt = new DataTable()
dt = dataset.Table["A"]
Now in this datatable there are columns a1,a2,a3,a4,a5,a6,a7.
Let's say I only want to get columns a3 and a4 then bind it to my datagrid.
How do I do this?
Hi Following code can be used
I tried this and it works.
I'd bind the whole table, then set up visibility of the coulmns as follows
I'd recommend reading this article from 4GuysFromRolla for anyone who needs a good understanding of the
DataGrid
Web Control.Note: Since this question is already answered. I want to clarify what needs to be done, just in case anyone else is wondering.
Ignore the fact that you have more data than you need. Set
AutoGenerateColumns
tofalse
. CreateBoundColumns
fora3
anda4
.You can always try to set DataPropertyName properties of particular columns to match what's in your DataTable. Then bind that DataTable to a BindingSource and bind that binging source to your grid.
As long as names of columns in your DataTable match DataPropertyNames of your DataGrid columns, your data grid should display only those matched columns.
In my example my stred proc does something simle like:
and my C# code:
Good luck!