使用数据库中的数据填充gridview

时间:2016-09-07 12:15:28

标签: c# html sql asp.net gridview

我有一个包含6个文本框的报告屏幕。我检查复选框,并在带有SQL查询的数据库中查找数据。当我点击按钮生成报告时,它不会在gridview中显示任何数据。我只是将gridview从我的工具箱粘贴到屏幕上,并希望用我的C#代码和SQL命令填充它。我错过了什么吗?

protected void Button1_Click(object sender, EventArgs e)
{
    connect = new SqlConnection(@"Data Source=LP12;Initial Catalog=SmmsData;Integrated Security=True");
    connect.Open();

    SqlCommand cmd = new SqlCommand();
    cmd.Connection = connect;
        cmd.CommandText = "SELECT DrukSensor, FlowSensor, TempSensor, KwaliteitSensor, Toerental, OlieVerbruik"
        + " FROM SysteemSensorInfo"
        + " WHERE DrukSensor=@DrukSensor"
        + " AND FlowSensor=@FlowSensor"
        + " AND TempSensor=@TempSensor"
        + " AND KwaliteitSensor=@KwaliteitSensor"
        + " AND Toerental=@Toerental"
        + " AND Olieverbruik=@OlieVerbruik"
        ;

    cmd.Parameters.Add("@DrukSensor", SqlDbType.Bit);
    cmd.Parameters.Add("@FlowSensor", SqlDbType.Bit);
    cmd.Parameters.Add("@TempSensor", SqlDbType.Bit);
    cmd.Parameters.Add("@KwaliteitSensor", SqlDbType.Bit);
    cmd.Parameters.Add("@Toerental", SqlDbType.Bit);
    cmd.Parameters.Add("@OlieVerbruik", SqlDbType.Bit);

    cmd.Parameters["@DrukSensor"].Value = CheckBoxDruk.Checked;
    cmd.Parameters["@FlowSensor"].Value = CheckBoxFlow.Checked;
    cmd.Parameters["@TempSensor"].Value = CheckBoxTemp.Checked;
    cmd.Parameters["@KwaliteitSensor"].Value = CheckKwaliteit.Checked;
    cmd.Parameters["@Toerental"].Value = CheckBoxToerenTal.Checked;
    cmd.Parameters["@OlieVerbruik"].Value = CheckBoxOlieVerbruik.Checked;

    DataSet ds = new DataSet();
    new SqlDataAdapter(cmd).Fill(ds);
    GridView1.DataSource = ds.Tables[0];
    GridView1.AutoGenerateColumns = true;
}

3 个答案:

答案 0 :(得分:1)

您尚未将data绑定到gridview。使用DataBind

将数据绑定到gridview
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();

答案 1 :(得分:0)

将BindData用于gridview,将所有数据绑定到gridview

DataSet ds = new DataSet();
new SqlDataAdapter(cmd).Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
GridView1.AutoGenerateColumns = true;

答案 2 :(得分:0)

试试这个:

connect = new SqlConnection(@"Data Source=LP12;Initial Catalog=SmmsData;Integrated Security=True");
    connect.Open();
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = connect;
     sqlCmd.CommandType = CommandType.Text;
        cmd.CommandText = "SELECT DrukSensor, FlowSensor, TempSensor, KwaliteitSensor, Toerental, OlieVerbruik"
        + " FROM SysteemSensorInfo"
        + " WHERE DrukSensor=@DrukSensor"
        + " AND FlowSensor=@FlowSensor"
        + " AND TempSensor=@TempSensor"
        + " AND KwaliteitSensor=@KwaliteitSensor"
        + " AND Toerental=@Toerental"
        + " AND Olieverbruik=@OlieVerbruik"
        ;
    cmd.Parameters.Add("@DrukSensor", SqlDbType.Bit);
    cmd.Parameters.Add("@FlowSensor", SqlDbType.Bit);
    cmd.Parameters.Add("@TempSensor", SqlDbType.Bit);
    cmd.Parameters.Add("@KwaliteitSensor", SqlDbType.Bit);
    cmd.Parameters.Add("@Toerental", SqlDbType.Bit);
    cmd.Parameters.Add("@OlieVerbruik", SqlDbType.Bit);
    cmd.Parameters["@DrukSensor"].Value = CheckBoxDruk.Checked;
    cmd.Parameters["@FlowSensor"].Value = CheckBoxFlow.Checked;
    cmd.Parameters["@TempSensor"].Value = CheckBoxTemp.Checked;
    cmd.Parameters["@KwaliteitSensor"].Value = CheckKwaliteit.Checked;
    cmd.Parameters["@Toerental"].Value = CheckBoxToerenTal.Checked;
    cmd.Parameters["@OlieVerbruik"].Value = CheckBoxOlieVerbruik.Checked;
    SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);.
    DataTable dtRecord = new DataTable();
    sqlDataAdap.Fill(dtRecord);
    GridView1.DataSource = dtRecord;       
    GridView1.AutoGenerateColumns = true;
}