Click here to Skip to main content
15,848,157 members
Please Sign up or sign in to vote.
3.50/5 (3 votes)
See more:
Only if value is "Paid" then only checkbox should be checked else it should unchecked.
I am trying this its making all the checkboxes checked.

protected void grdDataUpdate_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            CheckBox chk = (CheckBox)e.Row.FindControl("chkFIPaid");

            for (int i = 0; i < _dtStudentsFD.Rows.Count; i++)
            {
                if (_dtStudentsFD.Rows[i]["status"].ToString() == "Paid")
                {
                    chk.Checked = true;
                }
                else
                {
                    chk.Checked = false;
                }
            }
        }
    }
Posted
Comments
Laiju k 13-Nov-14 4:04am    
what is value in _dtStudentsFD.Rows[i]["status"].ToString()
jaket-cp 13-Nov-14 4:27am    
or what is the last value of _dtStudentsFD.Rows[i]["status"].ToString() as chk.Checked will be checked with that last value.
[no name] 13-Nov-14 4:37am    
Value is either "Paid" or "Pending"

SQL
Why do you use another for loop in RowDataBOund... RowDatabound means it will be called for each and every row...
Solution 1:
Place anothe column in your data grid which takes the value of "Status" and make it invisible and then write ur code like this



C#
protected void grdDataUpdate_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            CheckBox chk = (CheckBox)e.Row.FindControl("chkFIPaid");
             Label lblSatatus = (Label)e.Row.FindControl("lblStatusCol");
           
                if (lblSatatus.Text.ToString() == "Paid")
                {
                    chk.Checked = true;
                }
                else
                {
                    chk.Checked = false;
                }
            
        }
    }


Here when every column is being bounded the value of status and check box are accessed and then depending on Status col value of that row.. the check box status will be decided.

Please Accept as solution if it is useful to You
 
Share this answer
 
v2
Comments
[no name] 13-Nov-14 4:43am    
Thank you
protected void grdDataUpdate_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox chk = (CheckBox)e.Row.FindControl("chkFIPaid");
if (e.Row.Cells[i].Text == "Paid")
{
chk.Enabled = false;
}
else
{
chk.Enabled = true;
}
}
}
 
Share this answer
 
v2
look at your code.i think your doing something wrong with fallowing code.

C#
for (int i = 0; i < _dtStudentsFD.Rows.Count; i++)
 {
     if (_dtStudentsFD.Rows[i]["status"].ToString() == "Paid")
     {
        chk.Checked = true;
     }
     else
     {
        chk.Checked = false;
     }
}


Instead of above code , you should try with fallowing approach..
Here,'e.Row.Cells[5].Text' is the value of status field

C#
void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
  {
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
     CheckBox chk=(CheckBox)e.Row.cells[0].FindControl("chkMyCheckBox");
     if(chk != null)
     {
      if(e.Row.Cells[5].Text == ""status"") //check for status column
      chk.Checked = true;
     }
    }
  }
 
Share this answer
 
Here is an alternative technique that can be used to get the status value.
It should be available from the DataItem, instead of adding extra columns or controls to the GridView.
C#
void grdDataUpdate_RowDataBound(Object sender,GridViewRowEventArgs e) {
	if(e.Row.RowType == DataControlRowType.DataRow) {
		CheckBox chk = (CheckBox) e.Row.FindControl("chkFIPaid");

		DataRowView rowView = (DataRowView) e.Row.DataItem;
		String status = rowView["status"].ToString();

		if(status == "Paid") {
			chk.Checked = true;
		}
		else {
			chk.Checked = false;
		}
		//short hand if, ?: Operator
		//chk.Checked=(status == "Paid")?true:false;
	}
}

Check out: GridViewRow.DataItem Property[^]

The status checking on the CheckBox can also be performed on the aspx markup page:
ASP.NET
<asp:CheckBox ID="chkFIPaid" Checked='<%# (Eval("status") == "Paid") ? true : false %>' runat="server" />

Check out: ?: Operator[^]
Just for reference, VB.Net alternative to the "?: Operator": IIf Function[^]
 
Share this answer
 

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