【问题标题】:Gridview with DDl selected value带有 DDl 选定值的 Gridview
【发布时间】:2010-09-21 12:31:16
【问题描述】:

我有 gridview,它在加载时会从数据库中获取数据。我添加了选项让用户通过 DDl 过滤此网格视图我做了断点,我注意到 Gridview1.Databind() 对网格没有任何操作。所以请任何人帮助我

protected void Page_Load(object sender, EventArgs e)
{
    DataTable DT = new DataTable();

              if (DDlCity.SelectedIndex<0)
        {
            using (SqlConnection con = Connection.GetConnection())
            {
                SqlCommand Com = new SqlCommand("GetDealers", con);
                Com.CommandType = CommandType.StoredProcedure;

                SqlDataAdapter DA = new SqlDataAdapter(Com);
                DA.Fill(DT);
                GridView1.DataSource = DT;
                GridView1.DataBind();
            }

        }


    }

protected void DDlCity_SelectedIndexChanged(object sender, EventArgs e)
{
    DataTable DT = new DataTable();
    using (SqlConnection con = Connection.GetConnection())
    {
        SqlCommand Com = new SqlCommand("GetDealersByArea", con);
        Com.CommandType = CommandType.StoredProcedure;
        Com.Parameters.Add(Parameter.NewInt("@DEALERAREA_ID", DDlCity.SelectedValue));
        SqlDataAdapter DA = new SqlDataAdapter(Com);
        DA.Fill(DT);
        GridView1.DataSource = DT;
        GridView1.DataBind();
    }
}

【问题讨论】:

  • 当你说Gridview1.DataBind()没有任何动作时,你的意思是你的事件处理程序DDlCity_SelectedIndexChanged没有触发,或者当你调用DataBind()时它没有改变你的网格?
  • 另外,您是否在查询执行后检查了DataTable 的内容以确保您获得了结果集?
  • DataBind() 它没有改变网格的内容

标签: c# asp.net


【解决方案1】:

我想你对我说的话感到困惑......不用担心 这是您提供示例代码的工作示例。

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindGridFunction();
    }
}

private void BindGridFunction()
{
    try
    {
        DataTable DT = new DataTable();
        using (SqlConnection con = Connection.GetConnection())
        {
            if(DDlCity.SelectedIndex <0)
            {
                SqlCommand Com = new SqlCommand("GetDealers", con);
                Com.CommandType = CommandType.StoredProcedure;
            }
            else
            {
                SqlCommand Com = new SqlCommand("GetDealersByArea", con);
                Com.CommandType = CommandType.StoredProcedure;
                Com.Parameters.Add(Parameter.NewInt("@DEALERAREA_ID", DDlCity.SelectedItem.Value));
            }

            SqlDataAdapter DA = new SqlDataAdapter(Com);
            DA.Fill(DT);
            GridView1.DataSource = DT;
            GridView1.DataBind();
        }
    }
    catch(Exception ex)
    {
        DT = null; // etc...etc.. clear objects created
    }

}


protected void DDlCity_SelectedIndexChanged(object sender, EventArgs e)
{
    BindGridFunction();
}

我希望你明白我想说的话。您可以根据需要更改代码。

尚未测试,但我肯定会工作。

注意:我建议使用“DDlCity.SelectedItem.Value”而不是“DDlCity.SelectedValue”

【讨论】:

  • 我做了你发送的,但是当我选择 DDl 时,DataBind() 并没有改变网格的内容
  • 我想你需要检查 sp 是否返回你需要绑定的正确结果 >>>> Com.Parameters.Add(Parameter.NewInt("@DEALERAREA_ID", DDlCity.SelectedItem.Value ));你能得到你正在使用“GetDealersByArea”的sp代码吗?看看是否有问题......
  • Create proc [dbo].[GetDealersByArea] (@DEALERAREA_ID Int) AS SELECT Dealers.DEALER_ID, Dealers.NAME_ENG, DealerArea.AREA_ID, DealerArea.AREA_ENG, DealerArea.AREA_ARA FROM Dealers INNER JOIN DealerLocation ON Dealers .LOC_ID = DealerLocation.LOC_ID INNER JOIN DealerCategories ON Dealers.CAT_ID = DealerCategories.CAT_ID INNER JOIN DealerArea ON DealerLocation.AREA_ID = DealerArea.AREA_ID 其中 DealerArea.AREA_ID=@DEALERAREA_ID
  • 这个 sp 返回 DEALER_ID、NAME_ENG、AREA_ID、AREA_ENG AREA_ARA,其他 sp (GetDealers) 应该返回相同的字段???除此之外,问题可能是您需要将 @DEALERAREA_ID 值作为整数发送,因为 DDlCity.SelectedItem.Value 给出了字符串.....除此之外,请检查您声明下拉列表的代码 - 它的项目应该具有价值他们。
【解决方案2】:

在您的回帖中,您可以将绑定代码置于条件中

if (!IsPostBack){
 // Bind grid here looking for or used call to function something like BindGrid()
}

在 BindGrid() 函数中,您可以为网格视图编写绑定代码。

并且在 ddl selected index changed 事件上,您可以再次调用 BindGrid() 方法以再次进行相应的绑定。

还要检查您的下拉列表中是否有 EnablePostBack - true。

【讨论】:

  • 此函数受保护 DataTable GetGrid() { using (SqlConnection con = Connection.GetConnection()) { SqlCommand Com = new SqlCommand("GetDealersByArea", con); Com.CommandType = CommandType.StoredProcedure; Com.Parameters.Add(Parameter.NewInt("@DEALERAREA_ID", DDlCity.SelectedValue)); SqlDataAdapter DA = 新的 SqlDataAdapter(Com); DA.填充(DT); GridView1.DataSource = DT; GridView1.DataBind();返回 DT; } }
  • protected void DDlCity_SelectedIndexChanged(object sender, EventArgs e) { GetGrid(); }
  • protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { GetGrid(); GridView1.DataBind();}}
  • 但出现错误无法在函数中将参数值从字符串转换为 Int32
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-04
  • 1970-01-01
  • 1970-01-01
  • 2018-05-29
  • 1970-01-01
  • 2020-05-24
相关资源
最近更新 更多