|
-
November 30th, 2011, 04:59 AM
#1
[URGENT]how to get the value of the selected item of a datagridview combobox
hey all, I have a situation that needs a solution urgently.
so, the thing is I am trying to implementing a barcode reader into my C# application.
I understand that the barcode scanner works just like we type something on the keyboard.
I have a textbox which the user will scan the barcode. And it works just fine, the number appears on the textbox.
then I have a datagridview which has texboxes and a couple of comboboxes which are binded to the mysql database.
the problem is how do I get the barcode number inside the datagridview combobox to match exactly like the user's entered barcode inside the textbox?
please note that unlike the regular ComboBox control, the DataGridView ComboBox types doesn't have a SelectedItem/SelectedValue/SelectedIndex property for retrieving the currently selected object.
I've been working on this for almost a week and still no clue.
your help is much appreciated.
thanks
-
November 30th, 2011, 08:32 AM
#2
Re: [URGENT]how to get the value of the selected item of a datagridview combobox
way i found it in short is
1. identify desired combo boxs item's index
2. cbc.value = cbc.items[i];
example (start new form project and paste this as you form class)
Code:
public partial class Form1 : Form
{
DataGridView dgv;
public Form1()
{
InitializeComponent();
this.Load += new EventHandler(Form1_Load);
dgv = new DataGridView() { Width = 0300, Height = 300 };
this.Controls.Add(dgv);
this.Size = new Size(320, 340);
}
private void Form1_Load(object sender, EventArgs e)
{
dgv.Columns.Add(new DataGridViewComboBoxColumn());
dgv.Columns.Add(new DataGridViewTextBoxColumn());
dgv.CellClick += new DataGridViewCellEventHandler(dgv_CellClick);
DataGridViewComboBoxCell tempcbc = (DataGridViewComboBoxCell)dgv[0, 0];
tempcbc.Items.AddRange("val1", "val2", "val3"); //load some values
}
void dgv_CellClick(object sender, DataGridViewCellEventArgs e)
{
int c = e.ColumnIndex;
int r = e.RowIndex;
DataGridView tempdgv = (DataGridView)sender;
if (c < 0 || r < 0) return;
if (tempdgv[c, r] is DataGridViewComboBoxCell)
{
Random rand = new Random();
int i = rand.Next(4);
String[] values = new String[] { "val1", "val2", "val3", "otherval" };
MessageBox.Show("Im going to select : " + values[i]);
SelectValue((DataGridViewComboBoxCell)tempdgv[c, r], values[i]);
};
}
void SelectValue(DataGridViewComboBoxCell cbc, string value)
{ //Selects by name
for (int i = 0; i < cbc.Items.Count; i++)
{
if (cbc.Items[i].ToString() == value) //if value exist in combo box items list
{
cbc.Value = cbc.Items[i];
//sometimes you need to click elsewhere to visually update dgv
//due fact that combo box is selected right now. because of click event.
return;
}
}// here means its not found
string newname = "[New]" + value;
cbc.Items.Add(newname);
SelectValue(cbc, newname);
}
}
-
November 30th, 2011, 08:16 PM
#3
Re: [URGENT]how to get the value of the selected item of a datagridview combobox
thanks for the quick reply Nerfpl 
I will try to do it right now. will reply on the result ASAP
-
November 30th, 2011, 10:03 PM
#4
Re: [URGENT]how to get the value of the selected item of a datagridview combobox
 Originally Posted by Nerfpl
Code:
void dgv_CellClick(object sender, DataGridViewCellEventArgs e)
{
int c = e.ColumnIndex;
int r = e.RowIndex;
DataGridView tempdgv = (DataGridView)sender;
if (c < 0 || r < 0) return;
if (tempdgv[c, r] is DataGridViewComboBoxCell)
{
Random rand = new Random();
int i = rand.Next(4);
String[] values = new String[] { "val1", "val2", "val3", "otherval" };
MessageBox.Show("Im going to select : " + values[i]);
SelectValue((DataGridViewComboBoxCell)tempdgv[c, r], values[i]);
};
}
Ok, so I've tried this code. but this is not exactly what I meant Nerfpl...
in your code, the dgv_CellClick is active when you click any cell on the datagridview. and then the int c = e.ColumnIndex; int r = e.RowIndex; will count on which cell that you click.
what I meant is that after the user scan the barcode into the textbox (this part is OK), then the system will automatically search inside the database for the matching barcode number (for this part is OK too). I am using this code for the searching part:
Code:
private void barcodeTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
string synthetic_color_no = this.barcodeTextBox.Text;
this.synthetic_colorTableAdapter.FillByBarcode(asi_softwareDataSet.synthetic_color, synthetic_color_no);
this.synthetic_warehouseTableAdapter.Update(asi_softwareDataSet.synthetic_warehouse);
}
with this code, now the item inside my datagridview combobox is sorted & only shows the matching barcode number inside the dropdown menu.
my problem is: I want it to be automatically display/select the matching barcode number inside the datagridview combobox dropdown which is binded to the mysql database. so that the user doesn't have to click the combobox and select the number on their own. they would just scan & scan & scan.
thank you.
-
December 4th, 2011, 05:36 PM
#5
Re: [URGENT]how to get the value of the selected item of a datagridview combobox
Sorry but i kinda dont get it 
Combobox's values are on combobox.items[] as ObjectCollection
to make combobox 'select' some value you first need to find it by doing
Code:
foreach (object item in SomeComboBoxCell.Items)
{
if (item == "value to be set") { SomeComboBoxCell.Value = item; return; }
}
}
So if your user inputs code into textbox event fires and you apply this search&select to desired ComboBoxCell
you can also loop thru ComboBoxColumn to find desired cell instead using direct column,row.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|