[align=left]int releId = this.ddlUserType.SelectedIndex;
            this.gvUsers.DataSourceID = "odsUserByRoleId";
            if (this.gvUsers.Rows.Count == 0)
            {
                this.gvUsers.DataSourceID = "odsUser";
            }[/align]两个不同数据源ID: "odsUser"->查找所有用户; "odsUserByRoleId"->根据用户角色查找用户.
问题: 如果"odsUserByRoleId"数据源查找的结果为空则让它指定所有查找所有用户的数据源, 如何定义IF条件.

解决方案 »

  1.   

    那你直接判断odsUserByRoleId数据源为不为空,不就得了,如果为空,就用"odsUser"->查找所有用户,数据源绑定
      

  2.   

    你的if  怎么出问题了啊?我写了一个简单的, 你看看,
    这样替换数据源之前,首先要注意写上  
    this.GridView1.DataSource = null;
    确保数据源为空,不然会出错的/DataTable table = new DataTable();
            DataTable table1 = new DataTable();        DataColumn c1 = new DataColumn("id1");
            table.Columns.Add(c1);
            DataColumn c2 = new DataColumn("id2");
            table1.Columns.Add(c2);
           
            for (int i = 0; i < 2; i++)
            {
                DataRow r = table.NewRow();
                r[0] = i.ToString();
                table.Rows.Add(r);
            }
            if (table1.Rows.Count == 0)
            {
                this.GridView1.DataSource = null;
                this.GridView1.DataSource = table.DefaultView;
                this.GridView1.DataBind();
            }
            if (table.Rows.Count == 0)
            {
                this.GridView1.DataSource = null;
                this.GridView1.DataSource = table1.DefaultView;
                this.GridView1.DataBind();
            }