日期:2014-05-17  浏览次数:20477 次

根据查询条件生成动态GridView
不知道如何绑定动态的GridView,并且让他根据查询的条件变化而变化,标题也跟着变化,不知哪位大神能帮帮忙,不要百度的,如果是百度的,何须在这问你,就是GridView中的参数根据radioButton单选框进行变化,不用三层架构哦,如果意思不懂看我空间截图去

------解决方案--------------------
根据radioButton选择不同,产生不同的SQL语句参数,再从数据库中查询,再绑定GridView不就得了。
------解决方案--------------------
探讨
根据radioButton选择不同,产生不同的SQL语句参数,再从数据库中查询,再绑定GridView不就得了。

------解决方案--------------------
aspx:
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem>
loginDetail
</asp:ListItem>
<asp:ListItem>
userDetail
</asp:ListItem>
</asp:RadioButtonList>
<asp:Button ID="Button1" runat="server" Text="查询" onclick="Button1_Click" />
aspx.cs:
public partial class _Default : System.Web.UI.Page
{
string connString;
protected void Page_Load(object sender, EventArgs e)
{
connString = System.Configuration.ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt = GetData("loginDetail");
RadioButtonList1.SelectedIndex = 0;
GridView1.DataSource = dt;
GridView1.DataBind();
}
}

protected void Button1_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
int index = RadioButtonList1.SelectedIndex;
string table="";
if (index == 0)
{
table = "loginDetail";
//加载user表

}

else

//加载userDetail表
table = "userDetail";
}
dt = GetData(table);

GridView1.DataSource = dt;
GridView1.DataBind();
}

DataTable GetData(string table)
{
string command = "select * from"+" "+table;
DataSet ds = new DataSet();
DataTable dt = new DataTable();
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
SqlDataAdapter adapter = new SqlDataAdapter(command, conn);
adapter.Fill(ds);
dt = ds.Tables[0];
}

return dt;
}

}