C#

Moderators: None (Apply to moderate this forum)
Number of threads: 2722
Number of posts: 5749

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
DataGrid with only certain columns from a dataset Posted by GhostOfHendrix on 26 Nov 2004 at 3:02 PM
Hey everyone.
I would like to know how to display data from a dataset in a datagrid with only certain columns included from the dataset. I know how to display all of the dataset into a datagrid, but I would like to leave out some fields such as recordID and such that I dont want to display to the user. Can someone help?

This is how I fill the datagrid:

dataAdapter = new OleDbDataAdapter(CommandString, conn);
ds = new DataSet();
dataAdapter.Fill(ds, "prog");
dataTable = ds.Tables["prog"];
totalRec = dataTable.Rows.Count;

//Can I Bind Only certain columns of the dataset?
dgEvents.SetDataBinding(ds, "prog");
Report
Re: DataGrid with only certain columns from a dataset Posted by Xtreme.NET on 26 Nov 2004 at 8:26 PM
hi, what you can do is to make a datatable will the cols that you want to show in the datagrid then fill those cols of the datatable from your dataset...
after which u can bind that table to the datagrid...
will be very easy this way....
HTH...

"Rejoice always, pray without ceasing, in everything give thanks; for this is the will of God in Christ Jesus for you."[i]1Thess. 5:16-18[/i]

Report
Re: DataGrid with only certain columns from a dataset Posted by GhostOfHendrix on 26 Nov 2004 at 8:40 PM
Do you have some sample code that shows how to define the columns and how to fill them with the data? Thanks for the help!
Report
Re: DataGrid with only certain columns from a dataset Posted by Xtreme.NET on 29 Nov 2004 at 6:51 PM
_strSql = ""
Dim i As Short = 1
Dim dtFac As DataTable = New System.Data.DataTable("Fac")

objCommand = DbCore.GetCommand(_strSql)
objDbReader = DbCore.ExecuteReader(objCommand)

If DbCore.DBErrorNo <> 0 Then
ERROR!!!!!!
Else

dtFac.Columns.Add("Col1")
dtFac.Columns.Add("Col2")
dtFac.Columns.Add("Col4")
dtFac.Columns.Add("Col6")
dtFac.Columns.Add("Col10")
If Not objDbReader.Read Then

objDbReader.Close()
Exit Sub
Else
Do
Dim dRow As DataRow
dRow = dtFac.NewRow
dRow(0) = objDbReader.Item("Some Col that u need")
dRow(1) = objDbReader.Item("Col u need")
dRow(2) = objDbReader.Item("Col u need")
dRow(3) = objDbReader.Item("Col u need")
dtFac.Rows.Add(dRow)
Loop While (objDbReader.Read())
End If
objDbReader.Close()
myDatagrid.DataSource = dtFac
myDatagrid.DataBind()
End If

as u can see above i have use a data reader,
similarly, you can get the data in a dataset and then copy it over as in case of data reader like below :
Dim dRow As DataRow
dRow = dtFac.NewRow
dRow(0) = you dataset's col
dRow(1) = you dataset's col
dRow(2) = you dataset's col
dRow(3) = you dataset's col
dtFac.Rows.Add(dRow)


HTH....

"Rejoice always, pray without ceasing, in everything give thanks; for this is the will of God in Christ Jesus for you."[i]1Thess. 5:16-18[/i]




 

Recent Jobs