【问题标题】:How to populate checkbox ID based on the SQL Server column name it represents如何根据它所代表的 SQL Server 列名填充复选框 ID
【发布时间】:2015-09-05 01:25:40
【问题描述】:

SQL Server 表:

Name        Type1       Type2       Type3       Type4
-----------------------------------------------------
Lance       X                       X           X
John        X                       X
Mike                    X           X           X

ASP.net:

<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Auto Check" OnClick="Button1_Click" />

<asp:CheckBox ID="Type1" runat="server" /> Type1
<asp:CheckBox ID="Type2" runat="server" /> Type2
<asp:CheckBox ID="Type3" runat="server" /> Type3
<asp:CheckBox ID="Type4" runat="server" /> Type4

C#:

public void Button1_Click(object sender, EventArgs e)
{
    string strQuery = @"SELECT * FROM [Db1].[dbo].[Table1] ";
    strTxt = txtName.Text;
    strQuery += " WHERE [Name] = '" + strTxt + "'";

    using (SqlConnection sc = new SqlConnection(gloString))
    {
        try
        {
            sc.Open();
            SqlCommand scd = new SqlCommand(strQuery);
            scd.Connection = sc;

            SqlDataReader sdr = new SqlDataReader();
            sdr = scd.ExecuteReader();

            while (sdr.Read())
            {
                //use a foreach by column and check them?
            }
        }
        catch (SqlException)
        {
        }
        finally
        {
            sc.Close();
        }
    }
}

我知道我可以使用 if 语句来选中带有 X 的相应列的任何框。

相反,我如何创建一个 foreachfor 语句,它采用列名并填充与列名对应的复选框 ID,所以我不必使用很多 if 语句.

所以,如果我输入 Lance 作为名称并提交,Type1、Type3 和 Type4 将被自动检查。

【问题讨论】:

  • 您可以使用复选框列表。运行一个foreach,如果对应的值不为null,你可以在循环内检查它。

标签: c# sql asp.net sql-server


【解决方案1】:

试试这个:

    for (int i = 0; i < sdr.FieldCount; i++)
    {
        bool val = Convert.ToBoolean(dr[i]);
        string colName = sdr.GetName(i);
        if (colName != "Name" && val)
        {
            CheckBox myChkBox = (CheckBox)Page.FindControl(colName);
            myChkBox.checked = val;

        }
    }

【讨论】:

  • 那么如果 X 意味着它应该被检查,而空白意味着它不应该,那么上面的代码是否仍然有效?
  • 试过这样的事情:bool blVal = false; if (sdr[i] != DBNull.Value) { blVal = true; } 但我一直收到这个错误:Invalid attempt to read when no data is present.
  • 如果您在数据库中使用字符串值“X”而不是布尔值 true 或 false。那么首先,我会考虑改变这一点。如果该列的唯一可能值是“X”或 null。然后将列更改为默认值为 false 的布尔值更有意义,并且上述方法将起作用。否则 'bool blVal = (sdr[i] == DBNull.Value) ?假:真;'应该管用。确保所有代码都在您的“While(sdr.Read())”语句中,并且您可能需要在尝试读取之前检查以确保 sdr 有数据。 “如果(sdr.HasRows)”
  • 您的评论对您有所帮助。谢谢。
【解决方案2】:

在您要绑定的列的复选框上添加数据绑定,这样更快。

checkboxtype1.DataBindings.Add("Checked",Bindingsource~whatever source,"ColumnName");

if it is stored as a 1 or 0 你需要这个:

Binding bind = new Binding("Checked", bindingSource5, "Type1");
    bind.Format += (s,e) => {
           e.Value = (int)e.Value == 1;
        };
   CheckBoxType1.DataBindings.Add(bind);

或者:

//Retrieve column schema into a DataTable.
schemaTable = myReader.GetSchemaTable();

//For each field in the table...
foreach (DataRow myField in schemaTable.Rows){
    //For each property of the field...
    foreach (DataColumn myProperty in schemaTable.Columns) 
    {
    CheckboxTyp1.Checked =  (bool) myField[myProperty]);
    }

CheckboxType1 是您的 checkboxID,然后绑定简单地绑定到列名。类型1 所以如果你有四个复选框 CheckboxType1、CheckboxType2、CheckboxType3、CheckboxType4 然后将每一个绑定到相应的列,如果您在数据网格中有一个对象列表并且您的复选框在网格中,则有一种不同的绑定方式,但类似。

将 4 个复选框绑定到您的结果:

checkboxtype1.DataBindings.Add("Checked",Bindingsource~whatever source,"ColumnNameType1");
checkboxtype2.DataBindings.Add("Checked",Bindingsource~whatever source,"ColumnNameType2");
checkboxtype3.DataBindings.Add("Checked",Bindingsource~whatever source,"ColumnNameType3");

等等。 从您的代码问题来看,您似乎只想要一行数据。 名称 Type1 Type2 Type3 Type4

checkboxtype1 可以是 CheckBox1 或者 ChkBillyBob 的名字都没有关系。

[MyCheckBox].DataBindings.Add("Checked" ~ where Checked is a Property of the Control as a string name - Checkbox in this case has property Checked.
[MyCheckBox].DataBindings.Add("Checked", BindingSource ~ where binding source is your reader ; you should not need to iterate it unless you are expecting more than one result back. the while(sdr.Read()) can go just use the DataReader returned.
[MyCheckBox].DataBindings.Add("Checked",Bindingsource~whatever source,"ColumnNameType1" ~ ColumnNameType1 is the name of your column as a string hence it is in quotes'.

正如我所说,如果你有一个对象列表 - 它的工作方式有点不同但非常相似。您的预期结果/目的未完全说明。

【讨论】:

  • 我仍然需要指定每个单独的复选框 ID?
  • 看Gregs的回答我觉得比较清楚和完整的代码示例。
猜你喜欢
  • 2019-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多