思归大哥回复我的原帖如下:http://expert.csdn.net/Expert/topic/2322/2322132.xml?temp=.4995539

解决方案 »

  1.   

    而且我把这个
    <asp:BoundColumn Visible="False" DataField="zyid" ReadOnly="True" HeaderText="id"></asp:BoundColumn>加在<Columns> <asp:TemplateColumn>之间后,则这个页面所有东西都显示不出来了,而
    <asp:BoundColumn Visible="False" DataField="zyid" ReadOnly="True" HeaderText="id"></asp:BoundColumn>在另外一个例子上是好使的,
      

  2.   

    以下是思归大哥给的一个能正常运行的程序,我后来加上了一点东西<ItemTemplate>
    <asp:CheckBox id="Ckb_Sel" runat="server" Checked="False"></asp:CheckBox>
    </ItemTemplate>可是为什么这段 <ItemTemplate> </ItemTemplate>之间的内容不显示呢?
    <%@ Page Language="C#" %>
    <%@ import Namespace="System.Data" %>
    <%@ import Namespace="System.Data.SqlClient" %>
    <script runat="server">string sBoundField = "";void Page_Load(object o, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }void BindData()
    {
            SqlDataAdapter da = new SqlDataAdapter("select * from authors",
                            "server=localhost;database=pubs;uid=sa;pwd=;");        DataTable dt = new DataTable ();
            da.Fill(dt);        dg.DataSource = dt.DefaultView;
            dg.DataBind();
    }void dg_ItemDatabound(object source,   DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Header)
        {
            DropDownList ddl = (DropDownList)e.Item.FindControl("ddl");
            if (sBoundField == "")
               sBoundField = ddl.SelectedItem.Value;
            else
               ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue(sBoundField));
        }    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
           e.Item.Cells[0].Text = DataBinder.Eval(e.Item.DataItem,sBoundField).ToString();
        }}void TestSelection(object sender, EventArgs e)
    {
            DropDownList ddl = (DropDownList)sender;
            sBoundField = ddl.SelectedItem.Value;        BindData();}</script>
    <html>
    <head>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:DataGrid id="dg" runat="server" OnItemDataBound="dg_ItemDatabound" AutoGenerateColumns="False">
                <Columns>
                    <asp:TemplateColumn>
                        <HeaderTemplate>
                              <asp:CheckBox id="Ckb_All" runat="server"></asp:CheckBox>
                              <asp:DropDownlist id="ddl" runat="server" OnSelectedIndexChanged="TestSelection"    AutoPostBack="true">
                               <asp:ListItem Text="First Name" Value="au_fname" />
                               <asp:ListItem Text="Last Name" Value="au_lname" />
                              
                               </asp:DropDownList>
                        </HeaderTemplate>
                        <ItemTemplate>
                         <asp:CheckBox id="Ckb_Sel" runat="server" Checked="False"></asp:CheckBox>
                        </ItemTemplate>
                    </asp:TemplateColumn>
                </Columns>
            </asp:DataGrid>
            <asp:Button id="btn" runat="server" Text="Submit"></asp:Button>
        </form>
    </body>
    </html>
      

  3.   

    in my code, I was doinge.Item.Cells[0].Text = DataBinder.Eval(e.Item.DataItem,sBoundField).ToString();which replaces whatever in that column, what you are doing in your code?>>>则这个页面所有东西都显示不出来了open the HTML source in IE and check what's going on, one possibility is that you have unbalanced tags
      

  4.   

    我就是想要做一个这样的功能: http://www.rczx.com/siguidage.gif因为有的项是固定项,如 http://www.rczx.com/siguidage.gif 这个例子里的 “职位名称”这个字段,所以我要在 datagrid 中加入一些自定义比较灵活的字段<ItemTemplate>
    <asp:CheckBox id="Ckb_Sel" runat="server" Checked="False"></asp:CheckBox>
    <%# DataBinder.Eval(Container.DataItem,"职位名称") %></ItemTemplate>
    我现在就是不知道按你的方法如何显示别的字段,没有DropDownlist触发的固定字段
      

  5.   

    http://www.rczx.com/siguidage.gif 是http://wwww.51job.com 里查询功能所显示的结果,是php做的
      

  6.   

    remove
    e.Item.Cells[0].Text = DataBinder.Eval(e.Item.DataItem,sBoundField).ToString();but use<ItemTemplate>
    <asp:CheckBox id="Ckb_Sel" runat="server" Checked="False"></asp:CheckBox>
    <%# DataBinder.Eval(Container.DataItem, sBoundField) %>you can consider to change sBoundField to a public or protected property
      

  7.   

    <%# DataBinder.Eval(Container.DataItem,"职位名称") %>以上 "职位名称"是一个字段名称,<%# DataBinder.Eval(Container.DataItem,"职位名称") %> 中“职位名称”如何换成以下方法中的 sBoundField的值呢?
    ------------------------------------------------------------------------------
    public void dg_ItemDatabound(object source,   DataGridItemEventArgs e)
    {  
      string sBoundField = "";
      string sql = " select * from test_dropdown" ;    if (e.Item.ItemType == ListItemType.Header)
        {
          DropDownList ddl = (DropDownList)e.Item.FindControl("ddl");
          DataSet C1 = reg_data.dbbind( sql );
          ddl.DataSource=C1;
          ddl.DataTextField="dname";
          ddl.DataValueField="dname";
          ddl.DataBind();
           if (sBoundField == "")
           sBoundField = ddl.SelectedItem.Value;
           else
           ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue(sBoundField));
    }
      

  8.   

    1. make sBoundField a public or protected member or create a property for it, 2. remove this line
      string sBoundField = "";from dg_ItemDatabound3. change 
    <%# DataBinder.Eval(Container.DataItem,"职位名称") %>
    ===>
    <%# DataBinder.Eval(Container.DataItem,sBoundField) %>