Click here to Skip to main content
15,920,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

How to fill data into datagridview?i already fill the data into dataset where I name it dsReport.xsd and I want to build another sub that view all data that grab from dataset. How should I code that without declaring again the string connection? And I grab the database data using datareader where I'm not using any data adapter because I have a problem with it..

dgvReport = datagridview
dsReport = dataset
TblCashDetails = cash table

Anyone can help me?
Posted

1 solution

C#
public void PopulateGrid(GridView grdView, string sqlQuery, Page form)
    {
        try
        {
            adp = new SqlDataAdapter(sqlQuery, conn);
            tbl = new DataTable();
            adp.Fill(tbl);
            grdView.DataSource = tbl;
            grdView.DataBind();
        }
        catch (Exception ex)
        {
            MsgBox("Populate GridView Error: " + ex.Message, form);
        }
    }

    public void PopulateGrid(DataGrid dtGrd, string sqlQuery, Page form)
    {
        try
        {
            adp = new SqlDataAdapter(sqlQuery, conn);
            tbl = new DataTable();
            adp.Fill(tbl);
            dtGrd.DataSource = tbl;
            dtGrd.DataBind();
        }
        catch (Exception ex)
        {
            MsgBox("Populate DataGrid Error: " + ex.Message, form);
        }
    }

    public void PopulateGrid(DataList dtLst, string sqlQuery, Page form)
    {
        try
        {
            adp = new SqlDataAdapter(sqlQuery, conn);
            tbl = new DataTable();
            adp.Fill(tbl);
            dtLst.DataSource = tbl;
            dtLst.DataBind();
        }
        catch (Exception ex)
        {
            MsgBox("Populate DataList Error: " + ex.Message, form);
        }
    }
 
Share this answer
 
Comments
Luiey Ichigo 3-Nov-11 4:16am    
Is this will view all data that store in dataset?or it need to loop to find every details to be viewed?anyway, thanks for the code..
sahabiswarup 3-Nov-11 5:06am    
Above methods help to Populate grid for GridView , DataGrid as well as DataList

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900