从复选框列表中获取CheckedItems字符串值

时间:2018-12-11 12:13:53

标签: c# winforms

我想获取变量的checkedlistBox值,我的代码在下面,有人可以帮忙吗

private void CheckPreviousTxn_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            int selected = CheckPreviousTxn.SelectedIndex;
            string selecteditem1 = CheckPreviousTxn.CheckedItems.ToString();
            if (selected != -1)
            {
                foreach (string slecteditem in CheckPreviousTxn.CheckedItems)
                {
                    con = new SqlConnection(connectionpath);
                    cmd = new SqlCommand("select QTNCode as 'QTN Code',STKCODE as 'Item Code',STKDESCP as 'Item Name',Quantity,BaseUnit as 'Unit',Rate,DiscountAmount as 'Discount',Amount,VatPercentage as 'VAT %',TotalTaxAmount as 'VAT Amt' from Tbl_QTNDetail where QTNCode ='" + slecteditem + "'", con);
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    GrdQtnDetail.DataSource = dt;
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

我遇到以下异常

  

无法将类型为“ System.Data.DataRowView”的对象转换为类型为“ System.String”

我得到了checklistlistBox
value = {System.Windows.Forms.CheckedListBox, Items.Count: 13, Items[0]:QTN0001}

2 个答案:

答案 0 :(得分:2)

CheckedItems不是类型String的集合。您需要使用ListItem或使用Object,例如:

 foreach (ListItem selecteditem in CheckPreviousTxn.CheckedItems)
 {
    string itemValue = selectedItem.Value;
    string itemText = selectedItem.Text;
    string item = selectedItem.ToString();
    // use it in query
 }

或:

foreach(object itemChecked in checkedListBox1.CheckedItems) 
{
   string selectedItem= itemChecked.ToString();
}

请参阅DOCS here以供参考。

旁注:

不要对查询进行字符串连接。考虑使用参数化查询,因为您的应用程序容易受到带有字符串连接查询的sql注入的攻击。<​​/ p>

答案 1 :(得分:0)

另一件事,如果在for循环中填充数据表,则只会得到最后一个checkitem值数据表。 如果要将所有已阻塞项放在一个数据表中,则可以在for循环之前创建数据表,然后向该数据表中添加新数据:

    DataTable dtBig = new DataTable();
    foreach (ListItem selecteditem in CheckPreviousTxn.CheckedItems)
    {
       con = new SqlConnection(connectionpath);
       cmd = new SqlCommand("select QTNCode as 'QTN Code',STKCODE as 'Item Code',STKDESCP as 'Item Name',Quantity,BaseUnit as 'Unit',Rate,DiscountAmount as 'Discount',Amount,VatPercentage as 'VAT %',TotalTaxAmount as 'VAT Amt' from Tbl_QTNDetail where QTNCode ='" + selectedItem.ToString()+ "'", con);
       SqlDataAdapter da = new SqlDataAdapter(cmd);
       DataTable dt = new DataTable();
       da.Fill(dt);
       dtBig.Add(dt);
  }
  GrdQtnDetail.DataSource = dtBig;

或者您可以在sql中尝试使用性能更好的子句:

string[] items = CheckPreviousTxn.CheckedItems.OfType<object>().Select(item => item.ToString()).ToArray();
string resultItems = string.Join("','", items);
con = new SqlConnection(connectionpath);
cmd = new SqlCommand("select QTNCode as 'QTN Code',STKCODE as 'Item Code',STKDESCP as 'Item Name',Quantity,BaseUnit as 'Unit',Rate,DiscountAmount as 'Discount',Amount,VatPercentage as 'VAT %',TotalTaxAmount as 'VAT Amt' from Tbl_QTNDetail where QTNCode IN ('" + resultItems+ "')", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
GrdQtnDetail.DataSource = dt;