当前位置:编程学习 > C#/ASP.NET >>

GridView数据绑定求助

我现在有一个DropDownList控件,里面绑定了一些不同的产品类别,在同一个页面我添加了一个GridView控件来显示其中某一类别的产品的具体信息,现在我希望通过DropDownList选择不同类别的产品,相应的GridView显示对应类别产品的详细信息。注明:这些产品的字段都不一样,所以我每一类产品分别建了了一个表。请问我该怎么来实现这个功能呢? --------------------编程问答-------------------- 看看这个能不能帮到你
http://www.netcsharp.cn/showtopic-772.aspx --------------------编程问答-------------------- 谢谢你的回复,但是你给我的这个链接好像不行呢? --------------------编程问答-------------------- 这个问题应该不是很难吧,怎么高手都干什么去了啊? --------------------编程问答-------------------- 通过下拉框的条件查询数据库得到不同的数据源,然后将得到的数据源绑定到GridView上。直接通过代码获取数据源,字段不一样也没关系。而不是在设计状态设定GridView的列。 --------------------编程问答-------------------- <div>
         <asp:GridView ID="GridView1" 
        runat="server" 
        BackColor="White"
        BorderColor="#DEDFDE" 
        BorderStyle="None" 
        BorderWidth="1px" 
        CellPadding="4" 
        ForeColor="Black"
        GridLines="Vertical"
        OnRowCreated="GridView1_RowCreated">
            <Columns>
                <asp:TemplateField>
                 <HeaderTemplate>
                  <asp:DropDownList 
                   runat="server" 
                   ID="dropdownlist1"
                   BackColor="NavajoWhite"
                   AutoPostBack="true"
                   >
                  <asp:ListItem Value="0" Text=" "/>
                  <asp:ListItem Value="1" Text="整箱"/>
                  <asp:ListItem Value="2" Text="散货"/>
                  <asp:ListItem Value="3" Text="自拼"/>
                  </asp:DropDownList>
                 </HeaderTemplate>
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#CCCC99" />
            <RowStyle BackColor="#F7F7DE" />
            <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
            <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>
       </div>

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Test_RowFilter : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["connectionString"]);
    DataView dv = new DataView();
    DropDownList ddl;
    protected void Page_Load(object sender, EventArgs e)
    {
       if (!Page.IsPostBack)
       {
           BindData();
           
       }
   }
   void BindData()
    {
        string sql = "select top 20 JobNo,DeptName,JobType,InputDate,AttnName from viewjobse";
        conn.Open();
        SqlDataAdapter myda = new SqlDataAdapter(sql, conn);
        DataSet ds = new DataSet();
        myda.Fill(ds, "RusTable");
        dv = ds.Tables["RusTable"].DefaultView;
        GridView1.DataSource = dv;
        GridView1.DataBind();
        conn.Close();
    }
    
    protected void dropdownlist1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddl= sender as DropDownList;
        string selectedText = ddl.SelectedValue;
        BindData();
        switch (ddl.SelectedItem.Text)
        {
            case "整箱":
                dv.RowFilter = "jobtype='" + ddl.SelectedItem.Value + "'";
                GridView1.DataSource = dv;
                GridView1.DataBind();
                break;
            case "散货":
                dv.RowFilter = "jobtype='" + ddl.SelectedItem.Value + "'";
                GridView1.DataSource = dv;
                GridView1.DataBind();
                break;
            case "自拼":
                dv.RowFilter = "jobtype='" + ddl.SelectedItem.Value + "'";
                GridView1.DataSource = dv;
                GridView1.DataBind();
                break;
            default:
                BindData();
                break;
        }
    }

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
       if (e.Row.RowType == DataControlRowType.Header)
        {
            DropDownList ddl = (DropDownList)e.Row.FindControl("dropdownlist1");
            ddl.SelectedIndexChanged += new EventHandler(dropdownlist1_SelectedIndexChanged);
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[0].BackColor = System.Drawing.Color.White;
        }
    }

}


--------------------编程问答-------------------- 对啊  楼上的说的对 不要自定义列

当 droplist 的值改变事 执行一个 select 语句 让后绑定 一下 那个 gridview --------------------编程问答--------------------
引用 5 楼 antiking 的回复:
<div> 
          <asp:GridView ID="GridView1" 
        runat="server" 
        BackColor="White" 
        BorderColor="#DEDFDE" 
        BorderStyle="None" 
        BorderWidth="1px" 
        CellPadding="4" 
        ForeColor="Black" 
        GridLines="Vertical" 
        OnRowCreated="GridView1_RowCreated"> 
            <Columns> 
                <asp:TemplateField> 
        …

LZ看看应该可以用的 --------------------编程问答-------------------- GridView控件选中、编辑、取消、删除的功能代码 
后台代码:
你可以使用sqlhelper,本文没用。代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{

// http://www.dreamlandcn.com 
    SqlConnection sqlcon;
    SqlCommand sqlcom;
    string strCon = "Data Source=(local);Database=数据库名;Uid=帐号;Pwd=密码";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bind();
        }
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        bind();
    }

//删除
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string sqlstr = "delete from 表 where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
        sqlcon = new SqlConnection(strCon);
        sqlcom = new SqlCommand(sqlstr,sqlcon);
        sqlcon.Open();
        sqlcom.ExecuteNonQuery();
        sqlcon.Close();
        bind();
    }

//更新
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        sqlcon = new SqlConnection(strCon);
        string sqlstr = "update 表 set 字段1='"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',字段2='"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',字段3='"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where id='"
            + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
        sqlcom=new SqlCommand(sqlstr,sqlcon);
        sqlcon.Open();
        sqlcom.ExecuteNonQuery();
        sqlcon.Close();
        GridView1.EditIndex = -1;
        bind();
    }

//取消
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        bind();
    }

//绑定
    public void bind()
    {
        string sqlstr = "select * from 表";
        sqlcon = new SqlConnection(strCon);
        SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
        DataSet myds = new DataSet();
        sqlcon.Open();
        myda.Fill(myds, "表");
        GridView1.DataSource = myds;
        GridView1.DataKeyNames = new string[] { "id" };//主键
        GridView1.DataBind();
        sqlcon.Close();
    }
}

前台主要代码:
                            ... ...
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
                        ForeColor="#333333" GridLines="None" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"
                        OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit">
                        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                        <Columns>
                            <asp:BoundField DataField="身份证号码" HeaderText="用户ID" ReadOnly="True" />
                            <asp:BoundField DataField="姓名" HeaderText="用户姓名" />
                            <asp:BoundField DataField="员工性别" HeaderText="性别" />
                            <asp:BoundField DataField="家庭住址" HeaderText="家庭住址" />
                            <asp:CommandField HeaderText="选择" ShowSelectButton="True" />
                            <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
                            <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
                        </Columns>
                        <RowStyle ForeColor="#000066" />
                        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
                        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
                    </asp:GridView> --------------------编程问答-------------------- 选择时候更改数据
然后绑定
可以用自动生成列
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,