Reputation: 708
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
Upvotes: 2
Views: 3363
Reputation: 102448
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...
Upvotes: 2
Reputation: 5503
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".
Upvotes: 2
Reputation: 764
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") %>'>
Upvotes: 2