Click here to Skip to main content
15,894,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is UI
C#
<asp:GridView ID="gvSecurity" 
    runat="server" 
    AutoGenerateColumns="False" CellPadding="3" 
    OnPageIndexChanging="gvSecurity_PageIndexChanging"
    OnRowDeleting="gvSecurity_RowDeleting"
    onrowcommand="gvSecurity_RowCommand" 
    OnSelectedIndexChanged="gvSecurity_SelectedIndexChanged"
    AllowPaging="True"  
    HorizontalAlign="Justify" 
    Width="100%"
                    
    CssClass="avoGrid"
    PagerStyle-CssClass="avoGridPgr"
    AlternatingRowStyle-CssClass="avoGridAlt" AllowSorting="True">

    <AlternatingRowStyle CssClass="avoGridAlt"></AlternatingRowStyle>

        <Columns>                                                     
            <asp:BoundField DataField="szSecurityItemId" ItemStyle-HorizontalAlign="Left"  
                HeaderStyle-HorizontalAlign="Left" HeaderText="Security Item">                        
                <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
                <ItemStyle HorizontalAlign="Left"></ItemStyle>
            </asp:BoundField>
            <asp:BoundField DataField="szDescription" ItemStyle-HorizontalAlign="Left"  
                HeaderStyle-HorizontalAlign="Left"  HeaderText="Deskripsi">
                <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
                <ItemStyle HorizontalAlign="Left"></ItemStyle>
            </asp:BoundField> 
            <asp:templatefield HeaderText="Value">
                <itemtemplate>
                    <asp:CheckBox ID="ckbValue" runat="server" oncheckedchanged="gvSecurity_SelectedIndexChanged" CssClass="pkchkbox" Checked='<%# Eval("szValue") == DBNull.Value ? false : Convert.ToBoolean(Eval("szValue")) %>'></asp:CheckBox>
                </itemtemplate>
            </asp:templatefield>
        </Columns>        
    <PagerStyle CssClass="avoGridPgr"></PagerStyle>
</asp:GridView>

This is CodeProgram
C#
private DataTable m_dt;
int index;
CheckBox chk = null;

 foreach (DataRow dr in m_dt.Rows)
            {
                string szValue = (string)dr["szValue"];
                string szGroupId = (string)dr["szSecurityItemId"];
                item = new SecurityItemData();
                
                for (int i = 0; i < gvSecurity.Rows.Count; i++)
                {
                    chk = (CheckBox)gvSecurity.Rows[i].FindControl("ckbValue");
                    
                    
                    item.szSecurityItemId = (string)dr["szSecurityItemId"];
                    if (szValue.ToLower() == "true")
                    {
                        if (chk.Checked)
                        {
                            item.szValue = "N";
                        }
                        else
                        {
                            item.szValue = "Y";
                        }
                        
                    }
                    else
                    {
                        if (chk.Checked)
                        {
                            item.szValue = "Y";
                        }
                        else
                        {
                            item.szValue = "N";
                        }
                    }                //item.szValue = bVal ? "Y" : "N";
                }
                dat.itemList.Add(item);
            }
            return dat;
        }

  protected void gvSecurity_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Get the currently selected row using the SelectedRow property.
            GridViewRow row = ((GridViewRow)((CheckBox)sender).NamingContainer);
            index = row.RowIndex;
}

Please help me, because when i save value of checkbox doesn't not correspond to the selected checkboxes.
Posted
Updated 26-Nov-14 20:15pm
v2
Comments
Sinisa Hajnal 27-Nov-14 2:45am    
What do you get from the code? All true? All false? Are you sure you're not reseting the values (you have !Postback block, you don't refill / rebind your repeater, your viewstate on the page is on...).

Also, if you're using szValue as boolean variable it should be boolean field, not a word (string).
Member 10889447 27-Nov-14 2:54am    
I want to keep the value of the checkbox is checked that exist in the gridview, I use the string to be stored into the database in the form of a string, while true to be displayed in the interface in the form of a boolean
Sinisa Hajnal 27-Nov-14 3:13am    
I asked what you get, not what intend to do. And I'm saying you should have boolean in the database (since that is what the value represents) and if you need true on the interface, just call toString or make your own. Much easier then every time you need the comparison to cast it. And what happens when you need say...italian or spanish version? If the data is boolean, use boolean in the database, if it is date, put it into date field. DO NOT use string for everything. Trust me, you're creating more work for yourself, for any future developer and making it harder to compare, select, report etc...

Try like this..

C#
foreach (GridViewRow row in gvSecurity.Rows)
{
  CheckBox chk= row.FindControl("ckbValue") as CheckBox ;
//else CheckBox  chk=(CheckBox)row.FindControl("ckbValue");

if(chk!=null)
{
if (chk.Checked)
   {
    item.szValue = "Y";
   }
  else
   {
    item.szValue = "N";
   }
}

}
 
Share this answer
 
v2
In your code you have the following fragment:
C#
private DataTable m_dt;
int index;
CheckBox chk = null;
 
 foreach (DataRow dr in m_dt.Rows)
.
.
.


Since you're not setting m_dt you have no data (in fact, you don't have m_dt so it should fail with "object not set to an instance" error. Definitely, your for each here will NOT trigger and all the code inside it will never execute.

So, remove the outer for each and it should work.


If this helps please take time to accept the solution. Thank you.
 
Share this answer
 
Before this line:
C#
chk = (CheckBox)gvSecurity.Rows[i].FindControl("ckbValue");

add the following if statement,
C#
if (gvSecurity.Rows[i].RowType == DataControlRowType.DataRow)


If the problem is not solved, then the issue is not in the code you displayed

Also, what is the dat object your are returning, where are you using it?
 
Share this answer
 
Comments
Member 10889447 27-Nov-14 4:11am    
It's not solved. I have two rows checkbox whose value will be inserted into szValue. But when I check only one checkbox, both changed, not the checkbox is selected.

I'am using object dat in code.

private void p_BtnSave_Click()
{
try
{
SecurityData dat = p_UiToData();
if (m_objSecurity.IsSecurityExist(txtUserRoleID.Text, txtObjectID.Text))
m_objSecurity.UpdateSecurity(dat);
else
m_objSecurity.CreateSecurity(dat);

m_pg.ShowMessage("Budget successfully saved.", MessageType.Information);
}
catch (Exception ex)
{
ShowError(ex);
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900