0

我有一个 CheckedListBox。我希望能够从中选择 n 个项目,然后将这些项目值(作为 CheckedListBox 中的文本)传递到存储过程中。我对连接数据库和调用存储过程没有任何问题。我只需要弄清楚如何将选定的项目值分配给变量。我应该使用什么数据类型?

我想我需要弄清楚的是这部分:

string listingId = checkedListBoxBids.CheckedItems.ToString();

我在下面粘贴了我的代码。前 2 个方法调用我使用的存储过程;第三种方法基于一些单选按钮选择/单击确认按钮触发 2 个存储过程之一。

// marks selected listbox item as 'Won'
private void MarkItemAsWon(string itemWon)
{
    string listingId = checkedListBoxBids.CheckedItems.ToString();
    //string listingId = checkedListBoxBids.Text.ToString();

    // connection string
    string cnWatermelon = ConfigurationManager.ConnectionStrings["Watermelon.Properties.Settings.watermelonsConnectionString"].ToString();

    SqlConnection watermelonConn = new SqlConnection(cnWatermelon);

    SqlCommand markItemAsWonCommand = new SqlCommand();

    markItemAsWonCommand.CommandType = CommandType.StoredProcedure;
    markItemAsWonCommand.CommandText = "dbo.MarkItemAsWon";

    markItemAsWonCommand.Parameters.Add("@ListingID", SqlDbType.VarChar).Value = listingId;

    SqlDataAdapter MyDataAdapter = new SqlDataAdapter();
    DataTable dt = new DataTable();

    try
    {
        markItemAsWonCommand.Connection = watermelonConn;

        watermelonConn.Open();

        MyDataAdapter.SelectCommand = markItemAsWonCommand;

        MyDataAdapter.Fill(dt);
    }
    catch (Exception exc_PROCESS)
    {
        MessageBox.Show(exc_PROCESS.ToString(), "Error message",
        MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
    }
    finally
    {
        watermelonConn.Close();
    }
}


// marks selected listbox item as 'Lost'
private void MarkItemAsLost(string itemLost)
{
    string listingId = checkedListBoxBids.CheckedItems.ToString();
    //string listingId = checkedListBoxBids.Text.ToString();

    // connection string
    string cnWatermelon = ConfigurationManager.ConnectionStrings["Watermelon.Properties.Settings.watermelonsConnectionString"].ToString();

    SqlConnection watermelonConn = new SqlConnection(cnWatermelon);

    SqlCommand markItemAsLostCommand = new SqlCommand();

    markItemAsLostCommand.CommandType = CommandType.StoredProcedure;
    markItemAsLostCommand.CommandText = "dbo.MarkItemAsLost";

    markItemAsLostCommand.Parameters.Add("@ListingID", SqlDbType.VarChar).Value = listingId;

    SqlDataAdapter MyDataAdapter = new SqlDataAdapter();
    DataTable dt = new DataTable();

    try
    {
        markItemAsLostCommand.Connection = watermelonConn;

        watermelonConn.Open();

        MyDataAdapter.SelectCommand = markItemAsLostCommand;

        MyDataAdapter.Fill(dt);
    }
    catch (Exception exc_PROCESS)
    {
        MessageBox.Show(exc_PROCESS.ToString(), "Error message",
        MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
    }
    finally
    {
        watermelonConn.Close();
    }
}



// reads the option selected in the "Won?" groupbox and marks selected item as either 'Won' or 'Lost', then refreshes the checkedlistbox items
private void buttonWonConfirm_Click(object sender, EventArgs e)
{
    string listingId = checkedListBoxBids.CheckedItems.ToString();

    if (radioButtonWonYes.Checked == true)
    {

        //foreach (object itemChecked in checkedListBoxBids.CheckedItems)
        //{

        //    // show selected items in messagebox
        //    //MessageBox.Show("Item with title: \"" + itemChecked.ToString());

        //    MarkItemAsWon(itemChecked.ToString());
        //}


        MarkItemAsWon(listingId.ToString());

        PopulateBidItems();
        PopulateWonItems();
    }
    else
    {
        MarkItemAsLost(listingId.ToString());

        PopulateBidItems(); 
        PopulateWonItems();
    }
}
4

1 回答 1

1

checkedListBoxBids.CheckedItems将返回检查项目的集合。根据您的需要,您可以获得每个已检查的项目或仅以下项目之一

foreach(object itemChecked in checkedListBoxBids.CheckedItems)
{
     DataRowView item = itemChecked as DataRowView;
     string listingID= item["ListingID"];

}

假设你绑定如下

checkedListBoxBids.DataSource = dt;
checkedListBoxBids.DisplayMember = "ListingName";
checkedListBoxBids.ValueMember = "ListingID";
于 2013-08-13T17:56:07.303 回答