简体   繁体   中英

Assigning checkbox a value from gridview

I am selecting a row from Gridview , working good except for checkbox , like I am assigning value of checkbox retreived from gridview to checkbox that is placed on web form but it isn't represented by checkbox on form, it shows empty checkbox in every case

if (gridviewDesignations.SelectedRow.Cells[5].Text == " ")
{   
    chkIsHead.Text = gridviewDesignations.SelectedRow.Cells[5].Text;
}

in short, checkbox is not picking value from gridview

Update:

tried this too:

CheckBox chkIsHead = (CheckBox) gridviewDesignations.SelectedRow.Cells[5].Controls[0];
            if (chkIsHead.Checked == false)
            {
                chkIsHead.Checked = false;
            }
            else
            {

                chkIsHead.Checked = true;
            }

Update:

my full code:

public partial class frmDesignations : System.Web.UI.Page
{
    AccessibleVariables accessVariables = new AccessibleVariables(); //Used to access global variables
    public void Clear(params TextBox[] txtBoxes)
    {
        foreach (TextBox txtbx in txtBoxes)
        {
            txtbx.Text = "";
        }
    }


    public void fillddlDepartments()
    {
        ManageDepartmentsBizz mngDepBizz = new ManageDepartmentsBizz();
        DataSet ds = (DataSet)mngDepBizz.SelectDepartments();

        if (ds.Tables[0].Rows.Count != 0)
        {
            ddlDepartments.DataValueField = "DepID";
            ddlDepartments.DataTextField = "DepName";
            ddlDepartments.DataSource = ds.Tables[0];
           // ddlDepartments.SelectedIndex = -1;
            ddlDepartments.DataBind();
            ddlDepartments.Items.Insert(0, "--Select--");
        }
        //else
        //    ddlDepartments.SelectedIndex = -1;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session.Count <= 0)
        {
            Response.Redirect("login.aspx");
        }
        lblMsgPopUp.Visible = false;

        if (!IsPostBack) 
        {
            fillddlDepartments();
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            int DepartmentID = Convert.ToInt32(ddlDepartments.SelectedValue);
            bool IsHead = Convert.ToBoolean(chkIsHead.Checked);
            DesignationsBizz DesigBizz = new DesignationsBizz(-1, txtTitle.Text, DepartmentID, txtContactNo.Text, IsHead);
            //-1 is bogus,used to fill parameters criteria i.e no of params

            ManageDesignationsBizz mngDesigBizz = new ManageDesignationsBizz();
            bool Result = mngDesigBizz.Insert(DesigBizz);

            if (Result == true)
            {

                HiddenFieldSetMessage.Value = "Saved";
                HiddenFieldShowMessage.Value = "True";
                Clear(txtTitle, txtSelectedID, txtContactNo);
            }
            else
            {
                HiddenFieldSetMessage.Value = "RecordAlreadyExists";
                HiddenFieldShowMessage.Value = "True";
            }
        }
        catch (Exception)
        {
            HiddenFieldSetMessage.Value = "NotSaved";
            HiddenFieldShowMessage.Value = "True";
        }
    }
    protected void btnSearchPopup_Click(object sender, EventArgs e)
    {
        string DesignationTitle = txtDesignationPopUp.Text;
        ManageDesignationsBizz mngDepsBizz = new ManageDesignationsBizz();
        DataSet ds = (DataSet)mngDepsBizz.Select(DesignationTitle);

        if (ds.Tables[0].Rows.Count != 0)
        {
            lblMsgPopUp.Visible = false;
            gridviewDesignations.DataSource = ds.Tables[0];
            gridviewDesignations.DataBind();
            gridviewDesignations.Visible = true;
        }
        else
        {
            lblMsgPopUp.Visible = true;
            gridviewDesignations.Visible = false;
        }
    }
    protected void gridviewDesignations_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        string DesignationTitle = txtDesignationPopUp.Text;
        ManageDesignationsBizz mngDepBizz = new ManageDesignationsBizz();
        DataSet ds = (DataSet)mngDepBizz.Select(DesignationTitle);

        if (ds.Tables[0].Rows.Count != 0)
        {
            gridviewDesignations.PageIndex = e.NewPageIndex;
            gridviewDesignations.DataSource = ds.Tables[0];
            gridviewDesignations.DataBind();
            gridviewDesignations.Visible = true;
        }
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        if (gridviewDesignations.SelectedRow != null)
        {
            if (gridviewDesignations.SelectedRow.Cells[1].Text == "&nbsp;")
            {
                txtSelectedID.Text = string.Empty;
            }
            else
            {
                txtSelectedID.Text = gridviewDesignations.SelectedRow.Cells[1].Text;
            }
            if (gridviewDesignations.SelectedRow.Cells[2].Text == "&nbsp;")
            {
                txtTitle.Text = string.Empty;
            }
            else
            {
                txtTitle.Text = gridviewDesignations.SelectedRow.Cells[2].Text;
            }
            if (gridviewDesignations.SelectedRow.Cells[3].Text == "&nbsp;")
            {
                ddlDepartments.SelectedValue = string.Empty;
            }
            else
            {
                ddlDepartments.SelectedValue = gridviewDesignations.SelectedRow.Cells[3].Text;
                accessVariables.DepID = Convert.ToInt32(gridviewDesignations.SelectedRow.Cells[3].Text);
                ViewState["depID"] = accessVariables.DepID;
            }
            if (gridviewDesignations.SelectedRow.Cells[4].Text == "&nbsp;")
            {
                txtContactNo.Text = string.Empty;
            }
            else
            {
                txtContactNo.Text = gridviewDesignations.SelectedRow.Cells[4].Text;
            }

            CheckBox chkIsHead = (CheckBox) gridviewDesignations.SelectedRow.Cells[5].Controls[0];
            if (chkIsHead.Checked == false)
            {
                chkIsHead.Checked = false;
            }
            else
            {
                chkIsHead.Checked = true;
            }
            gridviewDesignations.DataBind();
            gridviewDesignations.SelectedIndex = -1;
            HiddenFieldShowHideButtons.Value = "True";
        }
    }
    protected void btnUpdatePopUp_Click(object sender, EventArgs e)
    {
        try
        {
            int id = Convert.ToInt32(txtSelectedID.Text);
            int DepartmentID = Convert.ToInt32(ddlDepartments.SelectedValue);
            bool IsHead = Convert.ToBoolean(chkIsHead.Checked);
            DesignationsBizz DesigBizz = new DesignationsBizz(id, txtTitle.Text, DepartmentID, txtContactNo.Text, IsHead );
            ManageDesignationsBizz mngDesigBizz = new ManageDesignationsBizz();
            bool Result = mngDesigBizz.Update(DesigBizz);

            if (Result == true)
            {
                HiddenFieldSetMessage.Value = "Updated";
                HiddenFieldShowMessage.Value = "True";
                Clear(txtSelectedID, txtTitle, txtContactNo);
            }
            else
            {
                HiddenFieldSetMessage.Value = "NotUpdated";
                HiddenFieldShowMessage.Value = "True";
            }
        }
        catch (Exception)
        {
            HiddenFieldSetMessage.Value = "NotUpdated";
            HiddenFieldShowMessage.Value = "True";
        }
    }
    protected void btnDeletePopUp_Click(object sender, EventArgs e)
    {
        try
        {
            int ID = Convert.ToInt32(txtSelectedID.Text.Trim());
            ManageDesignationsBizz mngDepBizz = new ManageDesignationsBizz();
            mngDepBizz.Delete(ID);
            Clear(txtTitle, txtSelectedID, txtContactNo);
            HiddenFieldSetMessage.Value = "Deleted";
            HiddenFieldShowMessage.Value = "True";
        }
        catch (Exception)
        {
            HiddenFieldSetMessage.Value = "NotDeleted";
            HiddenFieldShowMessage.Value = "True";
        }
    }
    protected void btnClosePopup_Click(object sender, EventArgs e)
    {
        Clear(txtTitle, txtSelectedID, txtContactNo);
    }
    protected void ddlDepartments_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (txtSelectedID.Text != "")
        {
            accessVariables.DepID = Convert.ToInt32(ddlDepartments.SelectedValue);
            ViewState["depID"] = accessVariables.DepID;
        }
        else 
        {
            accessVariables.DepID = Convert.ToInt32(ddlDepartments.SelectedValue);
            ViewState["depID"] = accessVariables.DepID;
        }



    }
    protected void chkIsHead_CheckedChanged(object sender, EventArgs e)
    {
        if (txtSelectedID.Text != "")
        {
            int DepID = Convert.ToInt32(ViewState["depID"]);
            ManageDesignationsBizz mngDesig = new ManageDesignationsBizz();
            bool isHead = mngDesig.SelectIsHeadExistsByDepID(DepID);

            if (isHead == true)
            {
                HiddenFieldSetMessage.Value = "HeadExists";
                HiddenFieldShowMessage.Value = "True";
                chkIsHead.Checked = false;
            }
        }
        else 
        {
            int DepID = Convert.ToInt32(ViewState["depID"]);
            ManageDesignationsBizz mngDesig = new ManageDesignationsBizz();
            bool isHead = mngDesig.SelectIsHeadExistsByDepID(DepID);

            if (isHead == true)
            {
                HiddenFieldSetMessage.Value = "HeadExists";
                HiddenFieldShowMessage.Value = "True";
                chkIsHead.Checked = false;
            }
        }


    }
}

I believe the following is what you need to do: 在此处输入图片说明

Then select EditProgrammatically.

I'm not sure if yoou'll need to manually put data into the grid though but that won't be too hard. Examples can be seen here : http://msdn.microsoft.com/en-us/library/system.data.datatable(v=vs.110).aspx

PS : You can set the DataSource in the grid view to the DataTable.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM