phan
phan

Reputation: 27

SQL query to datatable

I had done till one last part. Unsure how or what should I do to add into code.

public String DisplaySheetList()
{
    DataTable dt = new DataTable();
    GridView gv = new GridView();

    string sqlStatement1 = "SELECT cs.SheetId AS sheet_id, ltm.LocationType AS location_type, cs.PalletNo AS palletNo, cs.period AS period,cs.syncDate AS syncDate,cs.syncStatus AS syncStatus "
                + "FROM CountSheet cs JOIN LocationTypeMaster ltm ON ltm.LocationId = cs.LocationId " +
                " ORDER BY 1 DESC";
    SqlCommand sqlCmd1 = new SqlCommand(sqlStatement1, conn); 
    SqlDataAdapter sqlDa1 = new SqlDataAdapter(sqlCmd1);
    sqlDa1.Fill(dt);

     //What should i put here

     gv.DataBind();
}

I created the table and the view, got the query and fill it in to the table (if I am not wrong). Is it completed or is thing else I should do to ensure it is in the gridview so I can display them out.

Any help will be good, thank you guys

Upvotes: 0

Views: 209

Answers (2)

Jeric Cruz
Jeric Cruz

Reputation: 1909

So after you fill the result in your data table

Make dt the gridview datasource.

//put it in the gridview datasource.    
gv.DataSource = dt;

You can refer to this MSDN page for setting gridview datasource

To complete your code:

public String DisplaySheetList()
{
    DataTable dt = new DataTable();
    GridView gv = new GridView();

    string sqlStatement1 = "SELECT cs.SheetId AS sheet_id, ltm.LocationType AS location_type, cs.PalletNo AS palletNo, cs.period AS period,cs.syncDate AS syncDate,cs.syncStatus AS syncStatus "
                + "FROM CountSheet cs JOIN LocationTypeMaster ltm ON ltm.LocationId = cs.LocationId " +
                " ORDER BY 1 DESC";
    SqlCommand sqlCmd1 = new SqlCommand(sqlStatement1, conn); 
    SqlDataAdapter sqlDa1 = new SqlDataAdapter(sqlCmd1);
    sqlDa1.Fill(dt);

    //put it in the gridview datasource.    
    gv.DataSource = dt;

    gv.DataBind();
}

Upvotes: 1

Pankaj Toshniwal
Pankaj Toshniwal

Reputation: 197

Just Put :

gv.DataSource=dt;

in place of What should i put here to bind datatable data to gridview

Upvotes: 1

Related Questions