数据未从Radiobutton加载到GridView中

时间:2018-02-26 09:04:48

标签: c# asp.net sql-server

我有一个代码,我打算使用单选按钮从表中检索数据并将其加载到网格视图上。但是,一旦我点击单选按钮,GridView中就没有加载任何数据。以下是我的代码:

protected void Radiobuttonlist1_CheckedChange(Object sender, EventArgs e)
{
    string Value = Radiobuttonlist1.SelectedItem.Value.ToString();

    if (Value == "Patients")
    {
        string connect = TraceBizCommon.Configuration.ConfigSettings.ConnectionString;
        SqlConnection conn = new SqlConnection(connect);

        conn.Open();
        SqlCommand cmd = new SqlCommand("select PatientName,PatientCellPhone from CustomerInformation", conn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        conn.Close();
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    else if(Value== "Suppliers")
    {
        string connect = TraceBizCommon.Configuration.ConfigSettings.ConnectionString;
        SqlConnection conn = new SqlConnection(connect);

        conn.Open();
        SqlCommand cmd = new SqlCommand("select VendorName,VendorPhone from VendorInformation", conn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        conn.Close();
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    else if ( Value== "Employees")
    {
        string connect = TraceBizCommon.Configuration.ConfigSettings.ConnectionString;
        SqlConnection conn = new SqlConnection(connect);
        conn.Open();
        SqlCommand cmd = new SqlCommand("select EmployeeName,EmployeeMobilePhone from PayrollEmployees", conn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        conn.Close();
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }

}`

1 个答案:

答案 0 :(得分:2)

由于您使用的是DataSet而不是DataTable,因此您需要使用DataSet的{​​{3}}属性:

GridView1.DataSource = ds.Tables[0];

或者像这样:

da.Fill(ds,"tbl");
GridView1.DataSource = ds.Tables["tbl"];