2

I have an Editable Data grid which is pulling data from a SQL query one of the field has a Y or N answer and therefore needs to be a checkbox however in Edit Mode it shows as a field and errors if I put this in

<asp:CheckBox ID="ALFSUPR" runat="server" Checked='<%# Bind("pric_c_alfsupreq") %>'></asp:CheckBox>

Is there a simple way of converting the Text Field to a CheckBox which when Checked has a value = Y

Thanks

1
  • What kind of control you're using in the .aspx page? DataGrid or DataGridView? Commented Feb 21, 2012 at 21:34

3 Answers 3

2

Try something like this:

Checked='<%# Eval("pric_c_alfsupreq").ToString().Equals("Y") %>'>

UPDATE:

Since you're using an old DataGrid (you should be using DataGridView nowadays), you should have something similar to this in your DataGrid definition:

<asp:DataGrid ID="Grid" runat="server" PageSize="5" AllowPaging="True"
              DataKeyField="EmpId" AutoGenerateColumns="False" CellPadding="4"
              ForeColor="#333333" GridLines="No" 
              OnPageIndexChanged="Grid_PageIndexChanged"
              OnCancelCommand="Grid_CancelCommand" 
              OnDeleteCommand="Grid_DeleteCommand"
              OnEditCommand="Grid_EditCommand"
              OnUpdateCommand="Grid_UpdateCommand">

See the OnUpdateCommand...

Now the method that should run when you're applying an update to the row's data:

protected void Grid_UpdateCommand(object source, DataGridCommandEventArgs e)
{
    con = new SqlConnection(ConfigurationManager.AppSettings["connect"]);

    char value = "N"

    // You'll have to change the index here to point to the CheckBox you have in
    // your DataGrid.
    // It can be on index 1 Controls[1] or 2 Controls[2]. Only you know this info.
    if(((CheckBox)e.Item.Cells[0].Controls[0]).Checked == true)
    {
         value = "Y";;
    }

    cmd.Parameters.Add("@pric_c_alfsupreq", SqlDbType.Char).Value = value;

    cmd.CommandText = "Update command HERE";

    cmd.Connection = con;

    cmd.Connection.Open();

    cmd.ExecuteNonQuery();

    cmd.Connection.Close();

    Grid.EditItemIndex = -1;
}

Hope you get the idea. If you need this code in any other moment you can place it in the OnEditCommand, OnDeleteCommand, etc...

Sign up to request clarification or add additional context in comments.

5 Comments

This works it puts a checkbox into the form and thats great, however when I update it doesn't set the value but it doesn't error either any ideas?
I am struggling to understand this can you explain further I need to write a Y or N into the field on the database when I when i check the box or not
You have to convert the value back to your database. When the user check or uncheck the box it has true or false. You'll have to convert this value to Y or N so that it's like what you have in the DB. The DataGrid must have a OnUpdating event that should allow you to make the value conversion before sending it to the DB.
Hi thanks for the above it turns out I am using <asp:GridView> is the process the same as above?
Yes, but with the GridView you'd use the OnRowUpdating command: aspdotnet-suresh.com/2011/02/…
2

If your code requires a bit more logic than Simon's answer, you can also create a protected method in your code behind and call it.

protected bool GetCheckboxValue(String value)
{
    //put your logic here
    return value.ToLower() == "y";
}

and then call it with

Checked='<%# GetCheckboxValue("pric_c_alfsupreq") %>'>

Comments

2

Can't really remember my WebForms days, but possibly change the

Checked='<%# Bind("pric_c_alfsupreq") %>'>

to

Checked='<%# Eval("pric_c_alfsupreq") == "Y" %>'>

When you update, I think you'll have to handle the GridView.RowUpdating event, then extract the Checked property, convert it to "Yes" or "No".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.