如何将查询结果附加到数据表c#

时间:2017-03-11 05:23:47

标签: c# asp.net datatable

每次使用不同的参数调用方法时,我都需要将查询结果附加到datatable。但现在每次都覆盖datatable的内容而不是附加内容。

可以附加吗?请帮帮我......

Protected void chkrow_CheckedChanged(object sender, EventArgs e) 
{
    DataTable dtble = new DataTable();
    CheckBox chkrow = (CheckBox)sender;
    gridViewRow gvrow = chkrow.Parent.Parent;   
    GridView gvemp = (GridView) grdemp.Rows[gvrow.RowIndex].Cells[2].FindControl("gvemp");
    string emp = grdemp.DataKeys[gvrow.RowIndex].Values[2].ToString();
    if(gvemp.Checked == true)
    {
        string sqls = "select * from emptable where empid= ' " + emp.ToString()  + "' " ;
        OracleCommand cmd = new OracleCommand(sqls, cn) ;
        OracleDataAdapter da = new OracleDataAdapter(cmd) ;
        da.Fill(dtble);
        //here I need to append result to dtble rather than overwriting when each checkbox is checked
    } 

} 

1 个答案:

答案 0 :(得分:1)

在函数外部定义变量dtble以使其可全局访问,在页面加载时填充它,加载网格视图然后 -

Protected void chkrow_CheckedChanged(object sender,      EventArgs e) 
{
DataTable dtble_inner=new DataTable() ;
CheckBox chkrow=(CheckBox)sender;
gridViewRow gvrow=chkrow.Parent.Parent; 
GridView gvemp=(GridView) grdemp.                   Rows[gvrow.RowIndex].Cells[2].FindControl("gvemp");
string emp=grdemp. DataKeys[gvrow.RowIndex].Values[2].ToString();
if(gvemp.Checked==true)
{
    string sqls="select * from emptable where empid= ' " + emp. ToString()  +"' " ;
    OracleCommand cmd=new OracleCommand(sqls, cn) ;
    OracleDataAdapter da=new OracleDataAdapter(cmd) ;
    da. Fill(dtble_inner);
    if(dtble_inner.tables.count > 0 && dtble_inner.tables.rows.count >0)
     { 
         //something like below
         dtble.rows.add(dtble_inner.tables.rows(0));

     }
     // rebind your gridview here with dtble

} 

}