将gridview数据从一个表单复制到另一个表单的gridview

时间:2011-06-02 10:56:09

标签: c# asp.net gridview

protected void Page_Load(object sender, EventArgs e)
{
    Calendar1.Visible = false;
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection("connection string");
    con.Open();
    DataTable dt = new DataTable();
    SqlCommand sqlCmd = new SqlCommand("SELECT distinct plantname,operatorname FROM datalogging1 WHERE Line='" + ddlline.SelectedItem + "'and date='"+txtdate.Text+"'and shiftname='"+ddlshift.SelectedItem+"'", con);
    SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
    sqlDa.Fill(dt);

    if (dt.Rows.Count > 0)
    {
        //Where ColumnName is the Field from the DB that you want to display 
        txtplant.Text = dt.Rows[0]["Plantname"].ToString();
        txtop.Text = dt.Rows[0]["operatorname"].ToString();

    }
}
protected void btndate_Click(object sender, EventArgs e)
{
    Calendar1.Visible = true;
}
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
    txtdate.Text = Calendar1.SelectedDate.ToString("yyyy/MM/dd");
}
protected void btnreport_Click(object sender, EventArgs e)
{
    int var = ddlshift.SelectedIndex;
    switch (var)
    {
        case 0:
            break;
        case 1: SqlConnection con1 = new SqlConnection("connection string");
            con1.Open();
            DataTable dt = new DataTable();
            SqlCommand cmd1 = new SqlCommand("select Date=convert(varchar(12),isnull(t1.date,t2.date),101),Time=convert(varchar(8),isnull(t1.time,t2.time),8),isnull(t1.temprature,0) as Temperature,isnull(t2.pressure,0) as Pressure,isnull(t3.co2,0) as Co2,isnull(t4.do2,0) as Do2,isnull(t5.ph,0) as pH,isnull(t6.speed,0) as Speed,isnull(t7.flow,0) as Flow from temprature3 t1 full outer join pressure3 t2 on t2.date=t1.date and t1.time=t2.time full outer join co23 t3 on t1.date=t3.date and t1.time=t3.time full outer join do23 t4 on t1.date=t4.date and t1.time=t4.time full outer join ph3 t5 on t1.date=t5.date and t1.time=t5.time full outer join speed3 t6 on t1.date=t6.date and t1.time=t6.time full outer join flow3 t7 on t1.date=t7.date and t1.time=t7.time where t1.Date='"+txtdate.Text+"' and t1.time between '1900-01-01 7:00:00.000' and '1900-01-01 12:59:59.999'", con1);
            SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
            da1.Fill(dt);
            GridView1.Visible = true;
            GridView1.DataSource = dt;
            GridView1.DataBind();
            break;
        case 2:
            SqlConnection con2 = new SqlConnection("connection string");
            con2.Open();
            DataTable dt1 = new DataTable();
            SqlCommand cmd2 = new SqlCommand("select Date=convert(varchar(12),isnull(t1.date,t2.date),101),Time=convert(varchar(8),isnull(t1.time,t2.time),8),isnull(t1.temprature,0) as Temperature,isnull(t2.pressure,0) as Pressure,isnull(t3.co2,0) as Co2,isnull(t4.do2,0) as Do2,isnull(t5.ph,0) as pH,isnull(t6.speed,0) as Speed,isnull(t7.flow,0) as Flow from temprature3 t1 full outer join pressure3 t2 on t2.date=t1.date and t1.time=t2.time full outer join co23 t3 on t1.date=t3.date and t1.time=t3.time full outer join do23 t4 on t1.date=t4.date and t1.time=t4.time full outer join ph3 t5 on t1.date=t5.date and t1.time=t5.time full outer join speed3 t6 on t1.date=t6.date and t1.time=t6.time full outer join flow3 t7 on t1.date=t7.date and t1.time=t7.time where t1.Date='" + txtdate.Text + "' and t1.time between '1900-01-01 13:00:00.000' and '1900-01-01 22:59:59.999'", con2);
            SqlDataAdapter da2 = new SqlDataAdapter(cmd2);
            da2.Fill(dt1);
            GridView1.Visible = true;
            GridView1.DataSource = dt1;
            GridView1.DataBind();
            break;
        case 3: SqlConnection con3 = new SqlConnection(""connection string);
            con3.Open();
            DataTable dt2 = new DataTable();
            SqlCommand cmd3 = new SqlCommand("select Date=convert(varchar(12),isnull(t1.date,t2.date),101),Time=convert(varchar(8),isnull(t1.time,t2.time),8),isnull(t1.temprature,0) as Temperature,isnull(t2.pressure,0) as Pressure,isnull(t3.co2,0) as Co2,isnull(t4.do2,0) as Do2,isnull(t5.ph,0) as pH,isnull(t6.speed,0) as Speed,isnull(t7.flow,0) as Flow from temprature3 t1 full outer join pressure3 t2 on t2.date=t1.date and t1.time=t2.time full outer join co23 t3 on t1.date=t3.date and t1.time=t3.time full outer join do23 t4 on t1.date=t4.date and t1.time=t4.time full outer join ph3 t5 on t1.date=t5.date and t1.time=t5.time full outer join speed3 t6 on t1.date=t6.date and t1.time=t6.time full outer join flow3 t7 on t1.date=t7.date and t1.time=t7.time where t1.Date='" + txtdate.Text + "' and t1.time between '1900-01-01 23:00:00.000' and '1900-01-01 6:59:59.999'", con3);
            SqlDataAdapter da3 = new SqlDataAdapter(cmd3);
            da3.Fill(dt2);
            GridView1.Visible = true;
            GridView1.DataSource = dt2;
            GridView1.DataBind();
            break;
    }
}
public void ExportGridToExcel(GridView grdGridView, string fileName)
{
    Response.Clear();
    Response.AddHeader("content-disposition",
        string.Format("attachment;filename={0}.xls", fileName));
    Response.Charset = "";
    Response.ContentType = "application/vnd.xls";

    StringWriter stringWrite = new StringWriter();
    HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
    grdGridView.RenderControl(htmlWrite);
    Response.Write(stringWrite.ToString());
    Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
    /* Verifies that a Form control was rendered */
}

protected void Button1_Click(object sender, EventArgs e)
{
    ExportGridToExcel(GridView1, "report.xls");
}

1 个答案:

答案 0 :(得分:1)

由于您尚未提供代码,我们无法解决问题。

但下面的代码与Export GridView data to Excel完美配合,您可以使用此代码。

public void ExportGridToExcel(GridView grdGridView, string fileName)
{
Response.ClearContent();    
Response.AddHeader("content-disposition",
    string.Format("attachment;filename={0}.xls", fileName));
Response.ContentType = "application/vnd.xls";

StringWriter stringWrite = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
grdGridView.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.Flush();
Response.End();
}