Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

DataGrid with only certain columns from a dataset

GhostOfHendrixGhostOfHendrix Posts: 5Member
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");

Comments

  • Xtreme.NETXtreme.NET Posts: 112Member
    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...

    [size=1][COLOR=blue]"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][/color][/size]

  • GhostOfHendrixGhostOfHendrix Posts: 5Member
    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!
  • Xtreme.NETXtreme.NET Posts: 112Member
    _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....

    [size=1][COLOR=blue]"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][/color][/size]

Sign In or Register to comment.