locked
Can i change my textbox/label into a check box? RRS feed

  • Question

  • User-1928290767 posted

     

    i have a gridview that displays 1 and 0 (1 if the user has access and 0 if the user doesen't)

    if the user has an access to the roles , my problem is that my select query depends on how many roles the user wants to

    select, which makes my query dynamic. I use databind for every event of the gridview (updating,edit,cancel)

    My problem is that i want to display a check box instead of a label/textbox

     

    Sample Grid View 

     

    Any kind of help will be appreciated

    Thanks,

    Allen

     

     

    Wednesday, February 15, 2012 1:06 AM

Answers

  • User16212438 posted

    Had to tinker a bit to get a feel for it. Resulting demo:

    .aspx

            <asp:GridView runat="server" ID="GridView1"
                DataKeyNames="ID"
                AutoGenerateEditButton="true"
                OnRowCreated="GridView1_RowCreated"
                OnRowDataBound="GridView1_RowDataBound"
                OnRowEditing="GridView1_RowEditing"
                OnRowUpdating="GridView1_RowUpdating"
                OnRowCancelingEdit="GridView1_RowCancelingEdit" ></asp:GridView>
    

    .cs

        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dt;
            if (ViewState["data"] == null)
            {
                dt = new DataTable();
    
                dt.Columns.Add("ID");
                dt.Columns.Add("Description");
                dt.Columns.Add("Role_A");
                dt.Columns.Add("Role_B");
                dt.Columns.Add("Role_C");
                dt.Columns.Add("Role_D");
    
                DataRow dr = dt.NewRow();
                dr["ID"] = 1;
                dr["Description"] = "Description 1";
                dr["Role_A"] = false;
                dr["Role_B"] = false;
                dr["Role_C"] = false;
                dr["Role_D"] = false;
                dt.Rows.Add(dr);
    
                dr = dt.NewRow();
                dr["ID"] = 2;
                dr["Description"] = "Description 2";
                dr["Role_A"] = false;
                dr["Role_B"] = false;
                dr["Role_C"] = false;
                dr["Role_D"] = false;
                dt.Rows.Add(dr);
    
                dr = dt.NewRow();
                dr["ID"] = 3;
                dr["Description"] = "Description 3";
                dr["Role_A"] = false;
                dr["Role_B"] = false;
                dr["Role_C"] = false;
                dr["Role_D"] = false;
                dt.Rows.Add(dr);
    
                dr = dt.NewRow();
                dr["ID"] = 4;
                dr["Description"] = "Description 4";
                dr["Role_A"] = false;
                dr["Role_B"] = false;
                dr["Role_C"] = false;
                dr["Role_D"] = false;
                dt.Rows.Add(dr);
    
                ViewState["data"] = dt;
            }
            else
            {
                dt = (DataTable)ViewState["data"];
            }
    
            GridView1.DataSource = dt.DefaultView;
    
            if (!IsPostBack)
            {
                GridView1.DataBind();
            }
        }
    
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView gridView1 = (GridView)sender;
            gridView1.EditIndex = -1;
            gridView1.DataBind();
        }
    
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridView gridView1 = (GridView)sender;
            GridViewRow gridViewRow = gridView1.Rows[e.RowIndex];
            int id = int.Parse((String)gridView1.DataKeys[e.RowIndex].Value);
            DataTable dt = ((DataView)gridView1.DataSource).Table;
            DataRow dr = null;
            foreach (DataRow row in dt.Rows)
            {
                if (int.Parse((String)row["ID"]) == id)
                {
                    dr = row;
                    break;
                }
            }
    
            if (dr != null)
            {
                TextBox descriptionTextBox = (TextBox)gridViewRow.Cells[2].Controls[0];
                dr["Description"] = descriptionTextBox.Text;
    
                for (int i = 3; i < gridViewRow.Cells.Count; i++)
                {
                    if (gridViewRow.Cells[i] is DataControlFieldCell)
                    {
                        DataControlFieldCell cell = (DataControlFieldCell)gridViewRow.Cells[i];
                        CheckBox checkBox = (CheckBox)cell.Controls[0];
                        if (cell.ContainingField is BoundField)
                        {
                            BoundField boundField = (BoundField)cell.ContainingField;
                            dr[boundField.DataField] = checkBox.Checked;
                        }
                    }
                }
            }
    
            gridView1.EditIndex = -1;
            gridView1.DataBind();
        }
    
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView gridView1 = (GridView)sender;
            gridView1.EditIndex = e.NewEditIndex;
            gridView1.DataBind();
        }
    
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Replace(e.Row);
            }
        }
    
        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Replace(e.Row);
            }
        }
    
        private void Replace(GridViewRow gridViewRow)
        {
            // Skip Cells 0-2. Replace all others.
            for (int i = 3; i < gridViewRow.Cells.Count; i++)
            {
                if (gridViewRow.Cells[i] is DataControlFieldCell)
                {
                    DataControlFieldCell cell = (DataControlFieldCell)gridViewRow.Cells[i];
                    if (cell.ContainingField is BoundField)
                    {
                        BoundField boundField = (BoundField)cell.ContainingField;
    
                        cell.Text = null;
                        cell.Controls.Clear();
                        CheckBox checkBox = new CheckBox();
                        checkBox.ID = boundField.DataField;
                        checkBox.Enabled = (gridViewRow.RowState & DataControlRowState.Edit) == DataControlRowState.Edit;
                        if (gridViewRow.DataItem != null)
                        {
                            checkBox.Checked = Boolean.Parse((String)DataBinder.Eval(gridViewRow.DataItem, boundField.DataField));
                        }
                        cell.Controls.Add(checkBox);
                    }
                }
            }
        }
    

    Quite a bit of code. But it works.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 22, 2012 6:01 AM

All replies

  • User1274550967 posted

    Hi allen,

               May be this will help you..

               http://forums.asp.net/t/1767920.aspx/1?Change+Textbox+to+DropDown

     

    Wednesday, February 15, 2012 1:14 AM
  • User-568909048 posted

    If the only problem is while displaying then you you can take a checkbox and in the rowdataboundevent depending on the value in the label you can set that checkbox as checked or unchecked.

    Is it enough or you want the code how to execute it???

    Wednesday, February 15, 2012 1:15 AM
  • User-1928290767 posted

    message

    can you give me a sample snippet?

    Thursday, February 16, 2012 3:09 AM
  • User-1033639933 posted

    why not just build an ItemTemplate with binding to checkbox

    smth like this:

    <Columns>
    <asp:TemplateField HeaderText="Categories">
    <ItemTemplate>
    <asp:CheckBox 
        ID="chk1" 
        runat="server" 
    Checked='<%#Bind("isChecked") %>' />
    </ItemTemplate>
    
    </asp:TemplateField>
    
    </Columns>
    Thursday, February 16, 2012 3:20 AM
  • User-1928290767 posted

    why not just build an ItemTemplate with binding to checkbox

    smth like this:

    <Columns>
    <asp:TemplateField HeaderText="Categories">
    <ItemTemplate>
    <asp:CheckBox 
        ID="chk1" 
        runat="server" 
    Checked='<%#Bind("isChecked") %>' />
    </ItemTemplate>
    
    </asp:TemplateField>
    
    </Columns>

    My Gridview dosen't use itemtemplate , because i generate my select query in code behind , is it possible for me to use an item template?

    here's the code i use for populating the gridview

    Sub populateGVLB(ByVal EmployeeID As String)
            Dim setSql As String
            roleCount = 0
            setSql = "select  O_EI.EmployeeID as [EmpID],dbo.GetEmployeeName (O_EI.EmployeeID) as [EmpName] "
            Dim Item As ListItem
            For Each Item In lbChooseRoles.Items
                If Item.Selected = True Then
                    'While counter < ArrayList_RolesManageID.Count
                    setSql &= " ,ISNULL((select COUNT (*) from aspnet_UsersInRoles  where RoleId ='" & Item.Value & "' and aspnet_UsersInRoles.UserId = a_U.UserId ),0) as [ " & Item.Text & "]"
                    counter += 1
                    roleCount += 1
                End If
                'End While
            Next
            hfRoleCount.Value = roleCount
            setSql += " from OHCD_EmployeeInfo O_EI  inner join  aspnet_Users a_U on O_EI.EmployeeID = a_U.UserName where  O_EI.HCIS_Status_ID = 1 "
            setSql += " and a_U.ApplicationId = 'DD8C1061-5C2D-4BE2-8C33-9DBAD2599755' and O_EI.DepartmentID in ("
            Dim ItemDepartment As ListItem
            For Each ItemDepartment In lbDepartment.Items
                If ItemDepartment.Selected = True Then
                    setSql += ItemDepartment.Value & ","
                End If

            Next
            setSql += "0)"
            If EmployeeID <> "" Then
                setSql += "and O_EI.EmployeeID ='" & EmployeeID & "' "
            End If
            setSql += "order by 2"
            dsViewEmpRoles.SelectCommand = setSql

            GridView1.DataSource = dsViewEmpRoles
            GridView1.DataBind()

            '  GridView1.Visible = True
            hfSQL.Value = setSql

            hfRoleCount.Value = roleCount
        End Sub

    Thursday, February 16, 2012 4:03 AM
  • User-1033639933 posted

    then you'll need to work on RowDataBound event and add checkbox dynamically

    
    CheckBox chk = new CheckBox();
    chk.Checked = yourValue;
    
    e.Row.Cells[index].Controls.Add(chk);

    Thursday, February 16, 2012 4:17 AM
  • User16212438 posted

    Do you need editing? Or is it display-only?

    Monday, February 20, 2012 8:17 AM
  • User-1928290767 posted

    I need editing

    Monday, February 20, 2012 7:22 PM
  • User16212438 posted

    Had to tinker a bit to get a feel for it. Resulting demo:

    .aspx

            <asp:GridView runat="server" ID="GridView1"
                DataKeyNames="ID"
                AutoGenerateEditButton="true"
                OnRowCreated="GridView1_RowCreated"
                OnRowDataBound="GridView1_RowDataBound"
                OnRowEditing="GridView1_RowEditing"
                OnRowUpdating="GridView1_RowUpdating"
                OnRowCancelingEdit="GridView1_RowCancelingEdit" ></asp:GridView>
    

    .cs

        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dt;
            if (ViewState["data"] == null)
            {
                dt = new DataTable();
    
                dt.Columns.Add("ID");
                dt.Columns.Add("Description");
                dt.Columns.Add("Role_A");
                dt.Columns.Add("Role_B");
                dt.Columns.Add("Role_C");
                dt.Columns.Add("Role_D");
    
                DataRow dr = dt.NewRow();
                dr["ID"] = 1;
                dr["Description"] = "Description 1";
                dr["Role_A"] = false;
                dr["Role_B"] = false;
                dr["Role_C"] = false;
                dr["Role_D"] = false;
                dt.Rows.Add(dr);
    
                dr = dt.NewRow();
                dr["ID"] = 2;
                dr["Description"] = "Description 2";
                dr["Role_A"] = false;
                dr["Role_B"] = false;
                dr["Role_C"] = false;
                dr["Role_D"] = false;
                dt.Rows.Add(dr);
    
                dr = dt.NewRow();
                dr["ID"] = 3;
                dr["Description"] = "Description 3";
                dr["Role_A"] = false;
                dr["Role_B"] = false;
                dr["Role_C"] = false;
                dr["Role_D"] = false;
                dt.Rows.Add(dr);
    
                dr = dt.NewRow();
                dr["ID"] = 4;
                dr["Description"] = "Description 4";
                dr["Role_A"] = false;
                dr["Role_B"] = false;
                dr["Role_C"] = false;
                dr["Role_D"] = false;
                dt.Rows.Add(dr);
    
                ViewState["data"] = dt;
            }
            else
            {
                dt = (DataTable)ViewState["data"];
            }
    
            GridView1.DataSource = dt.DefaultView;
    
            if (!IsPostBack)
            {
                GridView1.DataBind();
            }
        }
    
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView gridView1 = (GridView)sender;
            gridView1.EditIndex = -1;
            gridView1.DataBind();
        }
    
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridView gridView1 = (GridView)sender;
            GridViewRow gridViewRow = gridView1.Rows[e.RowIndex];
            int id = int.Parse((String)gridView1.DataKeys[e.RowIndex].Value);
            DataTable dt = ((DataView)gridView1.DataSource).Table;
            DataRow dr = null;
            foreach (DataRow row in dt.Rows)
            {
                if (int.Parse((String)row["ID"]) == id)
                {
                    dr = row;
                    break;
                }
            }
    
            if (dr != null)
            {
                TextBox descriptionTextBox = (TextBox)gridViewRow.Cells[2].Controls[0];
                dr["Description"] = descriptionTextBox.Text;
    
                for (int i = 3; i < gridViewRow.Cells.Count; i++)
                {
                    if (gridViewRow.Cells[i] is DataControlFieldCell)
                    {
                        DataControlFieldCell cell = (DataControlFieldCell)gridViewRow.Cells[i];
                        CheckBox checkBox = (CheckBox)cell.Controls[0];
                        if (cell.ContainingField is BoundField)
                        {
                            BoundField boundField = (BoundField)cell.ContainingField;
                            dr[boundField.DataField] = checkBox.Checked;
                        }
                    }
                }
            }
    
            gridView1.EditIndex = -1;
            gridView1.DataBind();
        }
    
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView gridView1 = (GridView)sender;
            gridView1.EditIndex = e.NewEditIndex;
            gridView1.DataBind();
        }
    
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Replace(e.Row);
            }
        }
    
        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Replace(e.Row);
            }
        }
    
        private void Replace(GridViewRow gridViewRow)
        {
            // Skip Cells 0-2. Replace all others.
            for (int i = 3; i < gridViewRow.Cells.Count; i++)
            {
                if (gridViewRow.Cells[i] is DataControlFieldCell)
                {
                    DataControlFieldCell cell = (DataControlFieldCell)gridViewRow.Cells[i];
                    if (cell.ContainingField is BoundField)
                    {
                        BoundField boundField = (BoundField)cell.ContainingField;
    
                        cell.Text = null;
                        cell.Controls.Clear();
                        CheckBox checkBox = new CheckBox();
                        checkBox.ID = boundField.DataField;
                        checkBox.Enabled = (gridViewRow.RowState & DataControlRowState.Edit) == DataControlRowState.Edit;
                        if (gridViewRow.DataItem != null)
                        {
                            checkBox.Checked = Boolean.Parse((String)DataBinder.Eval(gridViewRow.DataItem, boundField.DataField));
                        }
                        cell.Controls.Add(checkBox);
                    }
                }
            }
        }
    

    Quite a bit of code. But it works.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 22, 2012 6:01 AM