Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将gridview数据从一个表单复制到另一个表单';s gridview_C#_Asp.net_Gridview - Fatal编程技术网

C# 将gridview数据从一个表单复制到另一个表单';s gridview

C# 将gridview数据从一个表单复制到另一个表单';s gridview,c#,asp.net,gridview,C#,Asp.net,Gridview,由于您没有提供代码,我们无法找出问题所在 但是下面的代码非常适合将GridView数据导出到Excel,您可以使用此代码 protected void Page_Load(object sender, EventArgs e) { Calendar1.Visible = false; } protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { SqlConnection con

由于您没有提供代码,我们无法找出问题所在

但是下面的代码非常适合
将GridView数据导出到Excel
,您可以使用此代码

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时,您确定网格是数据绑定的吗?我已经编辑了我的答案。只要复制并粘贴这个,让我知道你的发现。
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();
}