如何在CheckListBox中获取项目的ID

时间:2015-10-03 11:49:12

标签: c#

我的Windows窗体应用程序中有一个checkListBox,并使用SQL绑定了我的数据库数据。我根据我从comboBox中选择的内容填充项目列表。如果我从我的comboBox中选择第1项,它将显示客户'姓名谁正在使用此项目。我想要获得客户' id根据我在checkListBox中检查的内容。

以下是我填写comboBox的方法

 private void fill_comboBox()
        {
            myConn = new SqlConnection("Server = localhost; Initial Catalog= dbName; Trusted_Connection = True");
            try
            {
                string query = "select itemId, itemName from item_detail";
                SqlDataAdapter da = new SqlDataAdapter();
                myConn.Open();
                DataTable dt = new DataTable();

                SqlCommand command = new SqlCommand(query, myConn);

                SqlDataReader reader = command.ExecuteReader();

                dt.Load(reader);

                DataRow dr;
                dr= dt.NewRow();
                dr.ItemArray = new object[] { 0, "<----------Select an item-------> " };
                dt.Rows.InsertAt(dr, 0);

                itemComboBox.DataSource = dr;
                itemComboBox.ValueMember = "itemId";
                itemComboBox.DisplayMember = "itemName";

                fill_customerCheckListBox();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

以下是我填写checkListBox的方法

private void fill_customerCheckListBox()
{
    myConn = new SqlConnection("Server = localhost; Initial Catalog= dbName; Trusted_Connection = True");

    try
    {
        myConn.Open();

        DataSet myDataSet = new DataSet();
        myDataSet.CaseSensitive = true;

        string commandString = "select fullName from[customer_detail] as cd LEFT JOIN[item_customer] as ic ON ic.customerId= cd.customerI WHERE ic.itemId= '" + itemCombBox.SelectedValue + "' ";

        myCommand = new SqlCommand();

        myCommand.Connection = myConn;

        myCommand.CommandText = commandString;

        SqlDataAdapter myDataAdapter = new SqlDataAdapter();

        myDataAdapter.SelectCommand = myCommand;

        myDataAdapter.TableMappings.Add("Table", "customer_detail");

        myDataAdapter.Fill(myDataSet);

        DataTable myDataTable = myDataSet.Tables[0];

        customerCB.Items.Clear();

        string s = "Select All Customer";

        customer.Items.Add(s);

        foreach (DataRow dataRow in myDataTable.Rows)
        {

            customerCB.Items.Add(dataRow["fullName"]);
        }

    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

我有一个用于循环的praremter试图遍历来自customerCB的每个已检查项目,但这不起作用。当我处于调试模式时,代码DataRow row = (itemChecked as DataRowView).Row;导致异常

  

对象引用未设置为对象的实例。

我不知道如何解决这个问题。感谢帮助,谢谢

 string parameterList = ""; 



foreach (object itemChecked in customerCB.CheckedItems) 
{ 
     DataRow row = (itemChecked as DataRowView).Row; 
     parameterList += (parameterList.Length == 0) ? row[0].ToString() : ", " + row[0].ToString();
}

2 个答案:

答案 0 :(得分:2)

尝试以下方式!!

System.Data.DataSetExtensions

(OR)您需要将项目强制转换或解析为强类型的等效项,或使用foreach (var item in checkedListBox1.CheckedItems) { var row = (itemChecked as DataRowView).Row; int id = row.Field<int>("ID"); string name = row.Field<string>("fullName"); } 命名空间来使用下面演示的DataRowExtensions.Field method

var match = {};

var matches = [];

$('.SIsort').each(function (i, v) {
      console.log("type.."+typeof matches);
      var date = $(this).find('td:eq(0)').find('meta')[0].content;
      var team1 = $(this).find('td:eq(1)').find('div:eq(1)').text();
      var team2 = $(this).find('td:eq(1)').find('div:eq(3)').text();
      var loc = $(this).find('td:eq(2)').find('div:eq(0)').text();

      match.date = date;
      match.team1 = team1;
      match.team2 = team2;
      match.venue = loc;

      console.log(match); // It displays Correctly

      (matches = window.matches || []).push({});
      matches = (window.matches || []).push(match);
     // console.log(matches[i])

});

console.log(matches); // All object values belong only to final index

答案 1 :(得分:0)

似乎比你填充你的组合框时你正在输入dataRow["fullName"]因此这个演员DataRow row = (itemChecked as DataRowView).Row将不起作用,因为itemChecked只包含fullName的值,而不是该行,请尝试添加dataRow。