How to add a blank or custom value to the linked list?

I use System.Windows.Forms.Combobox linked to a category table as search criteria. I need to have the value "All" or an empty string that will be selected if the user does not want to use this criterion. Since combo is bound, each time it is clicked, the value added by combo1.Text = "All" is added. Obviously, I cannot add the Everyone category to the database. What is the best way to do this?

+3
source share
6 answers

Either manually add the “All” record to the linked dataset after loading other values, or untie the combo and instead move the data to fill it. This is just a search command, so you don't need all the benefits of binding.

+1
source
  • If you don’t want to add the Everyone item to your data source, you can do the following:

Code snippet

comboBox1.Text = "All";

It sets the text displayed in the comboBox to the assigned value, but without changing the elements in the comboBox and the associated data source.

  • You can also add "Everything" to your data source. But you should do it like this:

Code snippet

private void button1_Click(object sender, EventArgs e)

{

DataRow dataRow = dataTable.NewRow();

dataRow["ItemId"] = "All";

dataTable.Rows.InsertAt(dataRow, 0);

comboBox1.SelectedIndex = 0;

}

The easiest way is to insert a line into your ds.Tables [0], which

  ds = StockDAL.BindItemId();

  DataRow dataRow = ds.Tables[0].NewRow();

  dataRow["ItemId"] = "All";

  ds.Tables[0].Rows.InsertAt(dataRow, 0);

  comboBox1.DataSource = ds.Tables[0];
  comboBox1.DisplayMember = "ItemId";
  comboBox1.ValueMember = "ItemId";
  comboBox1.selectedIndex=0;

hope this solves ur problem

+5
source

:

ComboBox1.Items.Insert(0,"All");
+1
  • SQL-, ""
  • All, comboBox1.Items.Insert(0, "All" ), .

, .

0

"" . "" , , - , , .

0

ComboBox1.Items.Insert(0,"All");

ComboBox1 ,

if(ComboBox1.SelectedIndex>0)
{
//do the insert code here
}
else
{
//dont do anything
}

this will work fine ...

0
source

Source: https://habr.com/ru/post/1718108/


All Articles