How do i set a default comboBox Text? - c#

How do i set a default text for my combobox on loadup, such as "Select Something". I tried this on the Form Load
comboBox1.Text = "Select Something";
But that didnt work. Any help?

Your code is working for me, so you must be setting the text before binding the combobox to your data source. In the following test code it works if you have the cb.Text = "Test"; running after the databinding of the combobox, and displaying the same symptoms you are seeing if you are setting prior to the databind.
Consider the following code where there is just a combobox defined on the form (cb). If you move the last line to the top, it will display the symptoms you are seeing.
private void Form1_Load(object sender, EventArgs e)
{
DataTable tb = new DataTable();
tb.Columns.Add("Value");
tb.Rows.Add("Combo Box Item");
cb.DisplayMember = "Value";
cb.ValueMember = "Value";
cb.DataSource = tb;
cb.Text = "Select Something";
}

Related

ComboBox repeat the same value in DisplayMember and ValueMember

Hi i am trying to fill a combo box but the value member is the same as the displaymember
this is my code:
private void Form1_Load(object sender, EventArgs e)
{
refresh();
}
public void refresh()
{
using (var context = new AppDbContext())
{
var piece = context.piece.Select(d => d.quantity);
comboBox1.DataSource = piece.ToList();
comboBox1.DisplayMember = "quantity";
comboBox1.ValueMember = "quantity";
}
}
I receive this error message: system.argumentexception 'cannot bind to the new display member. parameter name newdisplaymember'
having only the DisplayMember and the valuemember as = "" I don't get any problem, but I need to show and save the same data, what is the problem with my code?
Have you tried to not set ValueMember? Maybe if you don't set that, the value will be the same as the display one.

How to get date time from textbox and put it into SQL Statement

I have the following code:
private void button3_Click(object sender, EventArgs e)
{
dataGridView1.Refresh();
dataGridView1.Columns.Clear();
clsMSSQL.clsMSSQL mssql = new clsMSSQL.clsMSSQL(2);
string sql = ("select CCase.RefNo AS Az, EventTemplate.EventCode AS Vorgang from ikaros.CCase join ikaros.Event on CCase.ID = Event.CCaseID join ikaros.EventTemplate on Event.EventTemplateID = EventTemplate.ID where EventTemplate.EventCode='IRVB' and Event.EventDate ='date' order by CCase.RefNo ASC");
mssql.Query(sql);
mssql.Fetch();
dataGridView1.ColumnCount = 2;
dataGridView1.Columns[0].Name = "Aktenzeichen";
dataGridView1.Columns[1].Name = "Vorgang";
while (!mssql.eof)
{
string[] arr_row = new string[2];
arr_row[0] = mssql.GetString("Az");
arr_row[1] = mssql.GetString("Vorgang");
dataGridView1.Rows.Add(arr_row);
mssql.Fetch();
}
dataGridView1.EndEdit();
dataGridView1.Refresh();
}
now I want to make a textbox where someone can put a datetime in and it gets the table of that datetime which was given in the textbox. I did my research and I found out that I need to make the following task a class:
and Event.EventDate ='date'
So 'date' should be the class which should get the information from the textbox and put it into the SQL Statement.
I tried to make my textbox do it but I always make the programm freeze.
Thank you.
You have to use a special control for this:
WPF: DateTimePicker for WPF 4.0
UWP: DateTimePicker
These controls, are embed able into Data-grid as well.

add tooltip to datagrid column filled from sqlite query

I am trying to add a tooltip to a specific column that may or may not be present, depending on the results of an sqlite query. The columnheaders are filled in from the query results.
I have this code to fill the datagrid:
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
SQLiteDataAdapter sda = new SQLiteDataAdapter(command);
sda.Fill(dt);
MainDataGrid.ItemsSource = dt.DefaultView;
I would like to do something like:
if(ColumnHeader == "Gauge")
{Column.Tooltip.Text = "Double click Gauge # to view details";} //Tooltip shows for entire column
But, I can't imagine it's that easy. I am completely self taught in c# and wpf, so a lot of my problems come from not knowing how to word the question properly.
I am not looking for a copy-paste answer. Just a link(s) and an explanation.
I used the following code:
void MainDataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
string tooltip = null;
switch (e.Column.Header.ToString())
{
case "Gauge":
tooltip = "Double click Gauge ID to view its details";
break;
}
if (tooltip != null)
{
var style = new Style(typeof(DataGridCell));
style.Setters.Add(new Setter(ToolTipService.ToolTipProperty, tooltip));
e.Column.CellStyle = style;
}
}
I got it here: Setting ToolTip for DataGridView automatically created columns
Thanks #Sinatr for getting me started in the right path.

How to Suggest Append ComboBox in DataGridView?

I have a ComboBox in a c# Windows forms application where I have set AutoCompleteMode to SuggestAppend, and the text is automatically appended to the input (Fig 1).
But if I set AutoCompleteMode to SuggestAppend in a DataGridView ComboBox it does not append the text (Fig 2).
How can I enable SuggestAppend in a datagridview combobox?
Fig 1 :
Fig 2 :
You'd think you'd do it just like the normal ComboBox:
this.comboBox1.AutoCompleteCustomSource = new AutoCompleteStringCollection();
this.comboBox1.AutoCompleteCustomSource.AddRange(new string[] { "Good night", "Good evening", "Good", "All Good", "I'm Good" });
this.comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
this.comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
With the expectant results:
As it turns out, you can! But the selected option won't persist once you leave the cell. I found you have to change how you add the drop-down options and how you source them:
public Form1()
{
InitializeComponent();
DataGridViewComboBoxColumn cc = new DataGridViewComboBoxColumn();
cc.Name = "Combo";
cc.Items.AddRange(new string[] { "Good night", "Good evening", "Good", "All Good", "I'm Good" });
this.dataGridView1.Columns.Add(cc);
}
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox box = e.Control as ComboBox;
if (box != null)
{
box.DropDownStyle = ComboBoxStyle.DropDown;
box.AutoCompleteSource = AutoCompleteSource.ListItems;
box.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
}
}
This will provide you the desired results:
Here is a quick example of how to use an AutoComplete ComboBox in DataGridView in a Windows Application.
Create one Windows Application and add DataGridView from toolbox to design.
Now create two DataGridViewComboBoxColumns and add them to the DataGridView:
public void ComboList1()
{
DataGridViewComboBoxColumn combo1 = new DataGridViewComboBoxColumn();
combo1.HeaderText = "Country";
combo1.Items.Add("Antarctica");
combo1.Items.Add("Belgium");
combo1.Items.Add("Canada");
combo1.Items.Add("Finland");
combo1.Items.Add("Albania");
combo1.Items.Add("India");
combo1.Items.Add("Barbados");
dataGridView1.Columns.Add(combo1);
}
public void ComboList2()
{
DataGridViewComboBoxColumn combo2 = new DataGridViewComboBoxColumn();
combo2.HeaderText = "Types of Jobs";
combo2.Items.Add("Accounting");
combo2.Items.Add("HR");
combo2.Items.Add("Finance");
combo2.Items.Add("Transportation");
combo2.Items.Add("Testing");
dataGridView1.Columns.Add(combo2);
}
Call both these methods from the Form Constructor.
Now Click on DataGridView and generate EditingControlShowing event and write the folllowing code in it:
if (e.Control is DataGridViewComboBoxEditingControl)
{
((ComboBox)e.Control).DropDownStyle = ComboBoxStyle.DropDown;
((ComboBox)e.Control).AutoCompleteSource = AutoCompleteSource.ListItems;
((ComboBox)e.Control).AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
}
This will work for all comboBoxes which are present in the DataGridView.
Got from this post.

WinForms ComboBox DropDown and Autocomplete window both appear

I've got a ComboBox on a winforms app with this code:
comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;
DataTable t = new DataTable();
t.Columns.Add("ID", typeof(int));
t.Columns.Add("Display", typeof(string));
for (int i = 1; i < 2000; i++)
{
t.Rows.Add(i, i.ToString("N0"));
}
comboBox1.DataSource = t;
comboBox1.ValueMember = "ID";
comboBox1.DisplayMember = "Display";
I then follow these steps when the window opens:
Click the ComboBox drop down button -- this displays the list of items and selects the text in the ComboBox
Type '5', '1' ... i.e. I'm looking to use autocomplete to search for 515, 516, etc.
You'll see that the autocomplete window now appears ON TOP of the drop down list. However if I mouse over, it's the obscured drop down window behind the autocomplete window that's receiving the mouse events, including the click. So I think I'm clicking on an autocomplete item but actually clicking on something totally random that I can't see.
Is this a bug in the ComboBox? I'm using Windows 7 if that matters. Am I configuring the ComboBoxwrong somehow?
Note also that using the KEYBOARD uses the autocomplete drop down. So up/down arrow keys are using the front window, but the mouse is using the back window.
Add a single line of code to your ComboBox KeyDown event and the problem is solved!
private void comboBox_NameAndID_KeyDown(object sender, KeyEventArgs e)
{
comboBox_NameAndID.DroppedDown = false;
}
Source
No problem getting a repro for this simply by setting the properties from the PropertyGrid. Behaves this way both in Win7 and Windows XP.
This is broken behavior documented in this feedback article. As indicated, Microsoft is not considering a fix. One possible workaround is to disable autocomplete in a DropDown event handler and re-enable it in a DropDownClosed event handler.
I'm a Brasilian student of encoding and I lose many hours seeking to fix it im my project. And here, I saw it in a few seconds!!!
My code seems like this:
private void populateCombos()
{
persist.ShowLst(dspMember, vlMember,varTable,lstBox,varWhere);
persist.ShowLst(dspMember, vlMember,varTable,ddlist1,varWhere);
persist.ShowLst(dspMember, vlMember,varTable, ddlist2,varWhere);
ddList1.Text = null;
ddList2.Text = null;
lstBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
lstBox.AutoCompleteSource = AutoCompleteSource.ListItems;
lstBox.Text = null;
}
Add to the/a keypress event.
Dim box As ComboBox = sender
box.DroppedDown = False
Select the ComboBox from the design view and set "Append" to the AutoCompleteMode property, this will suggest the item without apearing a window.
That's weired. Your code looks fine to me and I used this the AutoComplete feature a several times and it didn't show both the DropDown and the AutoComplete list.
My suggestion would be
Set the DataSource after the Display/Value Members. I can't remember why but the other caused some problems.
comboBox1.ValueMember = "ID";
comboBox1.DisplayMember = "Display";
comboBox1.DataSource = t;
Set the AutoCompleteSource at the end of your code (after adding the DataSouce)
Maybe that helps.
to only have one open at a time you can use comboBox1.Droppeddown = true open up the regular, false the AutoComplete will only appear
You simply add item in collection.
Now go properties option of combo box choose
AutoCompleteSource=ListItems
AutocompleteMode=suggest
note: autocomplete source have many option as per your requirement :)
WinForms ComboBox DropDown...the answer is this...
write below code in comboBox1 Enter event..
private void comboBox1_Enter(object sender, EventArgs e)
{
comboBox1.DroppedDown = true;
}
Now for comboBox1 AutoComplete...
write this AutoComplete() in page load event..so it work...
public void AutoComplete()
{
try
{
MySqlConnection conn = new
MySqlConnection("server=localhost;database=databasename;user
id=root;password=;charset=utf8;");
MySqlCommand cmd = new MySqlCommand("select distinct
(columnName) from tablename", conn);
DataSet ds = new DataSet();
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.Fill(ds, "tablename");
AutoCompleteStringCollection col = new
AutoCompleteStringCollection();
int i = 0;
for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
col.Add(ds.Tables[0].Rows[i]["columnName"].ToString());
}
comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
comboBox1.AutoCompleteCustomSource = col;
comboBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
Select the ComboBox from the design view and set "None" to the AutoCompleteMode property.

Categories

Resources