C# ASP.NET DataGrid 复选框

发布于 2025-01-07 08:15:33 字数 290 浏览 0 评论 0原文

我有一个可编辑数据网格,它从 SQL 查询中提取数据,其中一个字段有 Y 或 N 答案,因此需要是一个复选框,但是在编辑模式下,如果我将其放入

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

是否存在, 它会显示为一个字段并出现错误将文本字段转换为复选框的简单方法,选中时有一个值 = Y

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

等待我真够勒 2025-01-14 08:15:33

尝试这样的事情:

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

更新:

由于您使用的是旧的DataGrid(现在您应该使用DataGridView),所以您应该有类似的东西在您的 DataGrid 定义中:

<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">

请参阅 OnUpdateCommand...

现在,当您对行数据应用更新时应运行的方法:

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;
}

希望您明白了。如果您在任何其他时刻需要此代码,您可以将其放置在 OnEditCommandOnDeleteCommand 等中...

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...

携君以终年 2025-01-14 08:15:33

如果您的代码需要比 Simon 的答案更多的逻辑,您还可以在后面的代码中创建一个受保护的方法并调用它。

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

然后用

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

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") %>'>
凉宸 2025-01-14 08:15:33

不太记得我的 WebForms 日子,但可能会更改

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

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

当您更新时,我认为您必须处理 GridView.RowUpdating 事件,然后提取 Checked 属性,将其转换为“是”或“否”。

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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文