在GridView中显示下拉列表中所选项目的数据 [英] Display the data of dropdownlist selected item in gridview

查看:88
本文介绍了在GridView中显示下拉列表中所选项目的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试此代码以在gridview中显示下拉列表所选项目的数据,但仅在如果"条件有效而其他"条件无效的情况下.任何帮助将不胜感激.

Hi,

I am trying this code to display the data of dropdownlist selected item in gridview it''s working but only "if" condition is working and "else" condition is not. Any help would be appreciated.

protected void BtnSubmit_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConsString"].ConnectionString);

        //string sql = "Select * from ManstaSalary";
        //SqlCommand cmd = new SqlCommand(sql, con);


   if (DDlDepartment.SelectedValue != null)
    {
      con.Open();
      string str = DDlDepartment.SelectedValue;
            //string sql = "Select * from ManstaSalary where Department= ''" + str+"''" ;

      SqlDataAdapter ad = new SqlDataAdapter("Select * from ManstaSalary where Department= ''" + str + "''", con);
      DataSet ds = new DataSet();
      ad.Fill(ds);
      GridView1.DataSource = ds;
      GridView1.DataBind();
      con.Close();
        }

      else if (DDlDepartment.SelectedValue != null && DDLStaffid.SelectedValue != null)
        {
      con.Open();
      string str1 = DDlDepartment.SelectedValue;
      string str2 = DDLStaffid.SelectedValue;
            //string sql1 = "Select * from ManstaSalary where Department=''" + str1 + "''and StaffID=''" + str2+ "''";


      SqlDataAdapter ad = new SqlDataAdapter("Select * from ManstaSalary where  Department= ''" + str1 + "'' and StaffID = ''" + str2 + "''", con);
      DataSet ds = new DataSet();
      ad.Fill(ds);

      GridView1.DataSource = ds;
      GridView1.DataBind();
      con.Close();
        }
    }

推荐答案

anuradha_d写道:
anuradha_d wrote:

if(DDlDepartment.SelectedValue!= null)

if (DDlDepartment.SelectedValue != null)





anuradha_d写道:
anuradha_d wrote:

否则if(DDlDepartment.SelectedValue!= null&& DDLStaffid.SelectedValue!= null)

else if (DDlDepartment.SelectedValue != null && DDLStaffid.SelectedValue != null)



如果您注意到两个If语句之间的差异为DDLStaffid值.如果您了解有关If [



If you notice the difference between two If statements are of DDLStaffid value. If you read about If[^] statement execution then the first condition that is satisfied is executed.

Based on the current implementation, your code would either go into first if or just end of if. Else-If is never going to be executed.
If you really want that condition, then just reverse it. Use the two conditioned if first and then one conditioned.

Hope you got it.


UPDATE:
You definitely need to read on If statements if that was your requirement from start.

Ok. Now, if you want both of them to execute then it can be done in multiple ways. For example
OPTION 1: just separate them!

If(DDlDepartment.SelectedValue != null)
{
  // Do something 1
}

//followed by

if (DDlDepartment.SelectedValue != null && DDLStaffid.SelectedValue != null)
{
  // Do something 2
}


选项2:级联if's


OPTION 2: Cascade the if''s

if (DDlDepartment.SelectedValue != null)
{
  // Do soemthing 1

  if (DDLStaffid.SelectedValue != null)
  {
    // Do something 2
  }
}



可以使用其他方式来处理其他语句.现在,这应该为您做.



There can be other ways to handle using, other statements. For now, this should do for you.


这篇关于在GridView中显示下拉列表中所选项目的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆