|
Hi,
I have following function below but is not populating values from the table product?
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
database = new OleDbConnection(connectionString);
System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand("Select * from Products", database);
database.Open();
System.Data.OleDb.OleDbDataReader dr;
dr=cmd.ExecuteReader();
while (dr.Read())
{
comboBox3.Items.Add(dr["ProductName"]);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
regards,
|
|
|
|
|
- you probably want to clear out the old values from the combo box before you populate it with new data
- don't forget to close dr and database after you're finished using them otherwise you'll leak resources
- what happens when you step through this in the debugger? does your while loop properly iterate through all the rows in the DB?
- if the only column you're using from the DB table is ProductName then doing a Select * is rather inefficient.
- just for my own curiosity, why are you doing this in the SelectedIndexChanged event handler for the combo box?
Last modified: after originally posted -- grammar correction
|
|
|
|
|
Hi Jimmanuel,
...What else method or way u recommend instead of SelectedIndexChanged ? can u pls gimme example?
... I have 3 fields in that table so why it is inefficient?
i have changed code and following is here: to include dr.close method..
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
database = new OleDbConnection(connectionString);
System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand("Select distinct ProductName from Products", database);
database.Open();
System.Data.OleDb.OleDbDataReader dr;
dr = cmd.ExecuteReader();
while (dr.Read())
{
comboBox3.Items.AddRange(new Object[] { dr[1].ToString() });
}
dr.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
please help
|
|
|
|
|
How often is the DB Table going to change? If it doesn't change or rarely changes then you really only need to populate the combo box values once - when the form loads. If it does change frequently then there are lots of good ways to keep the combo box up to date. You could use a timer to reload the values every so often, put a button on the form that allows the user to refresh whenever they want or work out some way of receiving a notification any time that the table changes.
database.Close() is just as important. That closes the connection to the DB, otherwise it gets orphaned. A using[^] statement would be helpful in making sure that everything gets cleaned up whether an exception is thrown or not.
Say your table has 10 rows; that means that all you want from it is 10 product names but a select * statement is giving you back the 10 product names plus 20 other values that you aren't using. That's 3 times as much network traffic as you actually need. It might not be a big deal now but what if there were 10,000 rows? What if later on the table changes and more columns get added? For each column that's added that's more unnecessary traffic between your app and your DB and more time and memory required to handle your select . Select distinct ProductName is the way to go here. In fact, I can't recall a time that I've ever used select * .
|
|
|
|
|
why are you populating comboBox3 from db when something got selected on the same combobox?
You know showing any other thing except populating your combobox. You need to do this only once or when ever the data changes.
Yusuf
|
|
|
|
|
thanks Yusuf, i just want to see the product names in this comboBox thats all..and which will come from Products Table...
regards,
|
|
|
|
|
Good afternoon.
I am trying to reference the cell on a datagrid.
The Description column is hidden, but I want the description to populate a text box when the user clicks on a specific row.
I have the following so far:
rtxtDescription.Text = DataGridView1.Rows[index].Cells[2].Value.ToString();
The column is 2 and the row would depended on which row is selected.
I am using Visual Studio 2008.
Any suggestions?
WHEELS
|
|
|
|
|
CellClick event?
Time is the best teacher; unfortunately it kills all of its students.
जय हिंद
|
|
|
|
|
It is actually in a method:
public void PopulateDataGrid()
{
ds = new DataSet();
ds = d.FillDataGrid(this.cboDepartment.SelectedValue.ToString(), this.txtAcronym.Text);
DataGridView1.Columns.Clear();
if (ds != null && ds.Tables.Count != 0)
{
BindingSource bs = new BindingSource();
bs.DataSource = ds;
bs.DataMember = ds.Tables[0].TableName;
DataGridView1.DataSource = bs;
//return;
}
foreach (DataGridViewColumn col in DataGridView1.Columns)
{
if (col.HeaderText == constrDescrColumn) { col.Visible = false; }
}
//MessageBox.Show(DataGridView1.CurrentRow.Cells.IndexOf(.ToString());
int index = 0;
rtxtDescription.Text = DataGridView1.Rows[index].Cells[2].Value.ToString();
}
|
|
|
|
|
Wheels012 wrote: It is actually in a method:
In your first post you asked for suggestions and the reply was "CellClick event"
Now I could be wrong, but if I am correct it appears you have completely missed the point.
|
|
|
|
|
Good afernoon led mike.
I am very new to C# (huge VBA background).
My first hurdle is to populate the text box with the discription from the first cell from the description column.
Secondly I need to add this to the click event once a user changes from record to record.
WHEELS
|
|
|
|
|
you can use the indexer of the datagridveiw to directly find the cell you are looking for and also use the property "FormattedValue" to get the displayed text of the cell
str=dgrid[colIndex,rowIndex].FormattedValue.ToString();
I have to remind you,If you already don't know, that an empty cell means most of the time null, that is like a fuel to run time error, so be aware of that.
- try using the "RowEnter" event and access the selected row by the "DataGridViewCellEventArgs" in the event handler.
best of luck
|
|
|
|
|
Thank you everyone.
This is a great forum.
WHEELS
|
|
|
|
|
Good day people i have a little problem i hope you guys can help me
i have a windows form with some buttons and their flatstyle is set to Flatstyle.System, i used to have it set to
Flatstyle.Standard so i could put icons in them, but since i changed it to Flatstyle.System i cant put icons in them
is there any way i can put icons in buttons with FlatStyle.System?
thanks a lot in advance
|
|
|
|
|
Well, it always works on mine. I used Image property.
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKSQXUFYK[M`UKs*$GwU#(QDXBER@CBN%
Rs0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
--------------------------------------------------------
128 bit encrypted signature, crack if you can
|
|
|
|
|
Help me, I have Combobox in Datagriview i need bind combobox, but my code no bind it.
DataSet ds = new DataSet();
ds = neP.NE_Cargar_Procedimientos_Costos();
DataGridViewComboBoxColumn cbox = new DataGridViewComboBoxColumn();
foreach (DataGridViewRow row in dgvCostos.Rows)
{
if (dgvCostos.Columns[2].Name == "storedproc")
{
cbox.DataSource = ds.Tables[0].DefaultView;
cbox.DisplayMember = "name";
cbox.ValueMember = "id";
}
}
Help me
|
|
|
|
|
As far as I understood your code, you're trying to bind every Row's Combobox. But the fact is, its only one combobox associated with the column. So if you bind the combobox it will be same in every row.
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKSQXUFYK[M`UKs*$GwU#(QDXBER@CBN%
Rs0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
--------------------------------------------------------
128 bit encrypted signature, crack if you can
|
|
|
|
|
as it could then bind the combobox, i need bind all combobox, thank you Xmen
|
|
|
|
|
how can we apply self embedding water marking technique to images using vb.net or c#.net technique
|
|
|
|
|
first there are 2 buttons on the right-bottom in every message you post. One called 'Edit' and the second one 'Delete'. So if by mistake you posted 2 same messages you can use 'Delete' and to edit the message you can use 'Edit'. Simple as usual
As well as, here[^] is something for you
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKSQXUFYK[M`UKs*$GwU#(QDXBER@CBN%
Rs0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
--------------------------------------------------------
128 bit encrypted signature, crack if you can
|
|
|
|
|
how can i apply cosine transformation to an image using c#.net
|
|
|
|
|
Hi Folks, I have a string.."Desktop Development Web Development Multimedia Development". Now i want a 20 character string "Desk Dev Web Dev" like this...pls help
|
|
|
|
|
split the string with ' '(space char, e.g. string.split(' ')) and then iterate all the splitted parts, rip first 3 or 4 chars by using string.Substring() plus keep append a main string.
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKSQXUFYK[M`UKs*$GwU#(QDXBER@CBN%
Rs0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
--------------------------------------------------------
128 bit encrypted signature, crack if you can
|
|
|
|
|
There's a one-liner for everything.
words = string.Join(" ", words.Split().Select(s => s.Substring(0, 3)).ToArray());
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Guffa wrote: words = string.Join(" ", words.Split().Select(s => s.Substring(0, 3)).ToArray());
When did Microsoft put Select() in string.Split()
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKSQXUFYK[M`UKs*$GwU#(QDXBER@CBN%
Rs0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
--------------------------------------------------------
128 bit encrypted signature, crack if you can
|
|
|
|