Vikram
Vikram

Reputation: 215

How to loop multiple Data Tables and Perform print all in same page

I am having a grid view, in that user can select multiple tests with checkBox and finally take print.

I am unable to loop those selected tests in grid view. only the first test is printing every time.

Note: The Table Columns are static not Dynamic

pageload
{
    for (int i = 0; i < grdTests.Rows.Count; i++)
    {
        CheckBox chkTest = (CheckBox)grdTests.Rows[i].FindControl("chkTest");
        Label lblPtestid = (Label)grdTests.Rows[i].FindControl("lbltestid");
        string id= lblPtestid.Text;
        if (chkTest.Checked)
        {
            GetData(id)
            Print()
        }
        else
        {

        }
    }
}
public void GetData(string id)
{
   DataTable dt = Classfile.gettable(id);
   ViewSate["tables"] = dt;
}
public void Print()
{
   ViewSate["tables"].tostring(); //Sample Example
}


public static DataTable gettable(string id)
{
    string Query = "select * from table where id='"+id+"'";
    DataTable dt = DAL.getData(Query);
    return dt;
}

Upvotes: 0

Views: 1636

Answers (1)

Nikhil Vartak
Nikhil Vartak

Reputation: 5137

Instead of getting data for each selected record, why not 'get all' data at once and print a single data table which you are already doing. This also saves you hitting database for each selected record.

string csvId = string.Empty;

for (int i = 0; i < grdTests.Rows.Count; i++)
{
    CheckBox chkTest = (CheckBox)grdTests.Rows[i].FindControl("chkTest");
    Label lblPtestid = (Label)grdTests.Rows[i].FindControl("lbltestid");
    string id= lblPtestid.Text;
    if (chkTest.Checked)
    {
        csvId += id + ",";
        // GetData(id)
        // Print()
    }
    else {} // This is not required actually.
}

if(!string.IsNullOrEmpty(csvId))
{
    // Trim the extra comma at the end.
    csvId = csvId.Remove(csvId.Length - 1);

    // Get data for all selected records.
    GetData(csvId);
    Print();
}

public static DataTable gettable(string id)
{
    string Query = "select * from table where id IN ('" + id + "')";
    DataTable dt = DAL.getData(Query);
    return dt;
}

Note the IN clause used in select query in 'gettable' method.

Upvotes: 1

Related Questions