0

我有一个文本框和一个填充第一个 Gridview 的搜索按钮。之后,第二个按钮复制第二个网格中的第一个网格行。之后我应该能够在第一个网格中再次搜索并将其他结果附加到第二个网格。但我得到一个错误。提前谢谢。这是我的代码:

public partial class Grid: System.Web.UI.Page

{

DataTable Dt = new DataTable();
private DataTable Dt1
{
    set { ViewState.Add("Dt1", value); }
    get { return (DataTable)ViewState["Dt1"]; }
}


private void Fill_grid()
{
    string query = "Select * from table " +
    " where field1 like '" + TextBox1.Text + "' or filed2 like '" +TextBox1.Text + "'";       

    SqlConnection cnn = new SqlConnection(...);
    SqlCommand cmm = new SqlCommand(query,cnn);
    cmm.CommandType = System.Data.CommandType.Text;
    SqlDataAdapter MyDataAdapter = new SqlDataAdapter(cmm);
    DataSet DS = new DataSet();
    cnn.Open();
    MyDataAdapter.Fill(DS, "Client");
    Dt1 = DS.Tables["klient"];
    DataView dv = new DataView(Dt1);
    GridView2.DataSource = DS.Tables["klient"].DefaultView;
    GridView2.DataBind();


}
protected void Button2_Click(object sender, EventArgs e)
{
    Fill_grid();
}
protected void Button1_Click(object sender, EventArgs e)
{
    Fill_Second_Grid();
}

private void Fill_Second_Grid()
{

    DataRow dr;

    foreach (GridViewRow row in GridView2.Rows)
    {
        dr = Dt.NewRow();
            dr["Email"] = row.Cells[0].Text; ;
            Dt.Rows.Add(dr);
    }

    GridView3.DataSource = Dt;
    GridView3.DataBind();
}    

}

4

0 回答 0