سؤال

I have a CheckedListBox. I want to be able to select n items from it, and then pass those item values (as Text in the CheckedListBox) into a sproc. I have no issue with connecting to the DB and calling the sproc. I just need to figure out how to assign the selected item values to a variable. What data type should I use?

I think all I need to figure out is this part:

string listingId = checkedListBoxBids.CheckedItems.ToString();

I pasted my code below. The first 2 methods call the sprocs I use; the 3rd method fires one of the 2 sprocs based on some radio button selections / clicking on a confirm button.

// 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();
    }
}
هل كانت مفيدة؟

المحلول

checkedListBoxBids.CheckedItems will return collection of checked items. based on your need you can get each checked item or just one of item like below

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

}

assume you bind as below

checkedListBoxBids.DataSource = dt;
checkedListBoxBids.DisplayMember = "ListingName";
checkedListBoxBids.ValueMember = "ListingID";
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top