Link to home
Start Free TrialLog in
Avatar of speedbeeqs
speedbeeqs

asked on

Datagrid Custome Item

I need create a datagrid which have a column call Lock. That column view show as a list checkbox if value of column is 1 checkbox will show checked and 0 is unchecked.
ASKER CERTIFIED SOLUTION
Avatar of vinhthuy_nguyen
vinhthuy_nguyen

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of speedbeeqs
speedbeeqs

ASKER

I need C# code!
I converted it to C# but it has some wrong:

      public class ucMainUser : System.Web.UI.UserControl
      {
            WebClasses.WC_DB db = new WC_DB();
            protected System.Web.UI.WebControls.DataGrid dgUser;
            WebClasses.WC_HTTPContext context = new WC_HTTPContext();
            DataTable dt = new DataTable();

            private void Page_Load(object sender, System.EventArgs e)
            {
                  TemplateColumn cols = new TemplateColumn();
                  cols.HeaderText = "Lock";
                  cols.ItemTemplate = new DynamicItemTemplate();
                  if (!Page.IsPostBack)
                  {
                        db.ConnectingString = ConfigurationSettings.AppSettings["ConnectingString"];
                        dt = db.GetData("SELECT * FROM STREAM_USER");
                dgUser.DataSource = dt;
                        dgUser.Columns.Add(cols);
                        dgUser.DataBind();
                  }
            }

            #region Web Form Designer generated code
            override protected void OnInit(EventArgs e)
            {
                  //
                  // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                  //
                  InitializeComponent();
                  base.OnInit(e);
            }
            
            /// <summary>
            ///            Required method for Designer support - do not modify
            ///            the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                  this.Load += new System.EventHandler(this.Page_Load);

            }
            #endregion
      }
      public class DynamicItemTemplate : System.Web.UI.ITemplate
      {
            public void InstantiateIn(Control container)
            {
                  CheckBox oCheckBox = new CheckBox();
                  oCheckBox.DataBinding += new EventHandler(this.BindCheckBox);
                  container.Controls.Add(oCheckBox);
            }
            public void BindCheckBox(object sender,System.EventArgs e)
            {
                  CheckBox oCheckBox = (CheckBox)sender;
                  DataGridItem container = (DataGridItem)oCheckBox.NamingContainer;
      if (container.["IUSER_LOCKED"].GetType().ToString()=="System.DBNull")   <---Wrong here
                  {
                        oCheckBox.Checked = false;
                  }
                  else
      oCheckBox.Checked = (bool)container.DataSetIndex["IUSER_LOCKED"]; <--Wrong here
            }
                     }
nobody help me???!
I'm related in VB, :-(, but let's try DBNull.value.
Or container.["IUSER_LOCKED"] = System.DBNull.value 'or = ""
   
Toi nghi ban la nguoi VietNam nen toi ghi tieng Viet cho de~:
VB.net thi` la`: container.DataItem["IUSER_LOCKED"].DataType.ToString() = "System.DBNull.value"
nhung chuyen sang C# thi khong duoc. Ko biet chuyen nhu the nao`.
---
If you're not a Vietnamese:
VB.net is: container.DataItem["IUSER_LOCKED"].DataType.ToString() = "System.DBNull.value"
But when i convert to C# it will be wrong. I don't understand
oh sorry I type wrong:
VB.net is: container.DataItem("IUSER_LOCKED").DataType.ToString() = "System.DBNull.value"
But when i convert to C# it will be wrong. I don't understand
Hi, I'm Vietnamese, but to make it fair to the experts here, let's use English.
No, I mean 2 ways to check it
1. Container.Dataitem["ISDER_LOCKED"]= System.DBNull.value
2. Container.Dataitem["ISDER_LOCKED"].Getype.ToString = "System.DBNull"
Give it a try, buddy.
Must change it like this:
                  if (((DataRowView)container.DataItem)["IUSER_LOCKED"] == System.DBNull.Value)
                  {
                        oCheckBox.Checked = false;
                  }
                  else
                  {
                        oCheckBox.Checked = Convert.ToBoolean(((DataRowView)container.DataItem)["IUSER_LOCKED"]);
                  }
Thanks all experts
Happry programming.
Speedbeeqs : If there's something interesting, contact me at vinhthuy.nguyen@gmail.com