你那一列是什么字段,Bit 类型,还是 Boolean型,

解决方案 »

  1.   

    <asp:TemplateColumn ItemStyle-Wrap="False" ItemStyle-Width="22">
    <ItemTemplate>
    <asp:CheckBox Runat="server" Width="22" ID="chx_sel2" Enabled='<%# !((bool)DataBinder.Eval(Container.DataItem, "readonly")) %>' Checked='<%# ((bool)DataBinder.Eval(Container.DataItem, "selected")) %>' >
    </asp:CheckBox>
    </ItemTemplate>
    </asp:TemplateColumn>
      

  2.   

    readonly 和 selected 是你数据源的两列,这两列最好用bit,或者整型的0,1
      

  3.   

    是什么类型都可以~因为数据库是自己设计的 我想问再后台代码中怎么给checkbox设置绑定数据
      

  4.   

    顶一下。
    gridview在放一个控件试试
      

  5.   

     XmlDocument doc = new XmlDocument();
        protected void Page_Load(object sender, EventArgs e)
        {        for (int index = 1; index < GridView1.Columns.Count; index++)
            {
                GridView1.Columns[index].Visible = false;
            }        //调用MetadataService服务的SensorsQuery方法,返回所有的传感器        //XmlDocument doc = new XmlDocument();
            doc.LoadXml("<Satellites><Satellite satName='LANDSAT_5'><Sensors><Sensor sensorName='TM' extName='met' length='32768'/></Sensors></Satellite><Satellite satName='LANDSAT_7'><Sensors><Sensor sensorName='ETM' extName='met' length='32768'/></Sensors></Satellite><Satellite satName='ENVISAT'><Sensors><Sensor sensorName='ASAR' extName='N1' length='8100'/></Sensors></Satellite></Satellites>");        XmlNodeList satellite_nl = doc.DocumentElement.ChildNodes;
            for (int i = 0; i < satellite_nl.Count; i++)
            {
                XmlElement satNode = (XmlElement)satellite_nl[i];
                string satName = satNode.GetAttribute("satName");
                XmlNodeList sensor_nl = satNode.FirstChild.ChildNodes;
                for (int j = 0; j < sensor_nl.Count; j++)
                {
                    XmlElement senNode = (XmlElement)sensor_nl[j];
                    string senName = senNode.GetAttribute("sensorName");                //动态添加GridView列
                    TemplateField tf = new TemplateField();
                    tf.ShowHeader = true;
                    tf.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, satName + "_" + senName);
                    tf.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, satName + "_" + senName);
                    GridView1.Columns.Add(tf);
                    GridView1.DataSource = CreateDataSource();
                    GridView1.DataBind();
                }        }
        }
        ICollection CreateDataSource()
        {
            DataTable dt = new DataTable();
            DataRow dr;
            dt.Columns.Add(new DataColumn("id", typeof(Int32)));
            dt.Columns.Add(new DataColumn("UserName", typeof(string)));
            XmlNodeList satellite_nl = doc.DocumentElement.ChildNodes;
            for (int i = 0; i < satellite_nl.Count; i++)
            {
                XmlElement satNode = (XmlElement)satellite_nl[i];
                string satName = satNode.GetAttribute("satName");
                XmlNodeList sensor_nl = satNode.FirstChild.ChildNodes;
                for (int j = 0; j < sensor_nl.Count; j++)
                {
                    XmlElement senNode = (XmlElement)sensor_nl[j];
                    string senName = senNode.GetAttribute("sensorName");                dt.Columns.Add(new DataColumn(satName + "_" + senName, typeof(int)));
                }        }        SqlConnection con = new SqlConnection();
            con.ConnectionString = ConfigurationManager.AppSettings["ConnectionString"];        SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "select * from Users where UserState = '是'";        con.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            int userNum = 0;
            while (reader.Read())
            {
                dr = dt.NewRow();
                dr[0] = userNum+1;
                dr[1] = reader["UserName"].ToString();
                dr[2] = 0;
                dr[3] = 1;
                dr[4] = 0;
                dt.Rows.Add(dr);
                userNum++;
            }
            con.Close();
                    DataView dv = new DataView(dt);
            return dv;
        }
        public class GridViewTemplate : ITemplate
        {
            private DataControlRowType dcrType;
            private string columnName;
            public GridViewTemplate(DataControlRowType type, string colname)
            {
                dcrType = type;
                columnName = colname;
            }
            public void InstantiateIn(System.Web.UI.Control container)
            {
                switch (dcrType)
                {
                    case DataControlRowType.Header:
                        Literal literal = new Literal();
                        literal.Text = columnName;
                        container.Controls.Add(literal);
                        break;
                    case DataControlRowType.DataRow:
                        CheckBox cb = new CheckBox();
                        cb.ID = columnName;
                        cb.Text = "";
                        cb.AutoPostBack = true;
                        //cb.Checked = Convert.ToInt32(DataBinder.Eval(Container.DataItem, "columnName")) == 1 ? true : false;
                        container.Controls.Add(cb);
                        break;
                    default:
                        break;
                }
            }
        }这是我的代码
      

  6.   

     #region"権限より編集ボタンの状態を処理する"
        /// <summary>
        /// 権限より編集ボタンの状態を処理する
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvQuestionlist_RowDataBound1(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex > -1)
            {
                String strUserId = Convert.ToString(Session["USER_ID"]);
                String strUserType = Convert.ToString(Session["USER_TYPE_ID"]);
                DataRowView rv = (DataRowView)e.Row.DataItem;
                // 説明を取得する
                if (rv["QUESTION_MEMO"].ToString().Length != 0)
                {
                    Label label = (Label)e.Row.FindControl("Label1");
                    label.Text =lblSubstr(rv["QUESTION_MEMO"].ToString(), 38);
                }            //
                if ( rv["QUESTION_END"].ToString() == "是" || Convert.ToInt32 (rv["ANSWER_COUNT"].ToString()) >0)
                {
                    ((LinkButton)e.Row.Cells[11].Controls[0]).Enabled = false;               
                }            // 管理者ではない
                if (strUserType != ConfigurationManager.AppSettings["USER_TYPE_01"])
                {
                    CheckBox chkDelete = (CheckBox)e.Row.FindControl("chkDelete");
                    chkDelete.Enabled = false;
                                    // 自分のリソースではない場合
                    if (rv["CREATOR"].ToString() != strUserId)
                    {
                        ((LinkButton)e.Row.Cells[11].Controls[0]).Enabled = false;
                    }
                }
            }
        }
        #endregion
      
    *****************************************************************************
    欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html
      

  7.   

    这些列都是动态生成的~怎么在前台写啊?
    前台代码:
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
                    <Columns>
                        <asp:BoundField HeaderText="用户名" DataField="UserName" />
                    </Columns>
    </asp:GridView>????怎么弄?????
      

  8.   

    可以在 grid 的 RowDataBound 方法中设置,
    根据不用的值控制checkbox是否被选中。DataRowView data = (DataRowView)e.Row.DataItem; // 获取数据行
    CheckBox ck0 = (CheckBox )e.Row.FindControl("ck0"); // ck0 是模版列中的CheckBox的ID
    if (data["column0"].ToString() == "1")
        ck0.Checked = true; ....保存的时候也要反方向判断一次才可以。
      

  9.   

    nj_1st_excellence 兄弟的方法可以`
    问题是一翻页就出错~~不知怎么解决~~如果能帮我解决 ~我可以再发帖放分
      

  10.   

    可以通过两张表联接,绑定就行了。如何你的选项是BOOL型的。