|
This question has been asked, in one way or another, a few times and I've yet to see a solid answer.
I've searched the articles and messages but don't see a solution.
Here's my desire:
A normal ComboBox, when the DropDownStyle is set to ComboBox rather than ComboBoxList, will allow the user to select an item from the Items list OR type in their own value.
Near as I can tell, the DataGridView ComboBox performs validation to see if the Value matches an item in the list. If that Value is not found in the list, you get the error message:
"System.ArgumentException: DataGridViewComboBoxCell value is not valid"
Basically making the DataGridViewComboBoxCell function like a ComboBoxList -- in essence NOT allowing the user to type in a value that isn't in the list.
Is there a way around this? Is there a way for a DataGridViewComboBoxCell to allow the user to EITHER select a Value from the list OR type in a new value that is NOT already in the list?
I am definitely NOT the only one out here with this question / problem, so if you have any ideas on a way around it, PLEASE let us all know.
Thanks a bunch.
|
|
|
|
|
I found a little more info on this problem searching MS. Here's a comment:
The DataGridViewComboBox must hold all possible values that it will display
in it's items collection. For example, when binding to the Customers table
and having the CompanyName field a combobox the combobox must contain all
possible values. If the grid attempts to set the cell's value where the
combobox cell doesn't have the value then we throw the dataError event.
To workaround this, handle the DataError event of the DataGridView. For
example:
void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs
e)
{
// ignore the data error occurring under the territoryComboBoxColumn
if (e.ColumnIndex != territoryComboBoxColumn.Index)
{
MessageBox.Show("Data error occurs:" + e.Exception.Message);
}
}
This tells us how to avoid the Exception. However, it doesn't address allowing the user's typed input to become the value for that cell.
Any thoughts?
|
|
|
|
|
Did you try this?
if (e.ColumnIndex != territoryComboBoxColumn.Index)
{
MessageBox.Show("Data error occurs:" + e.Exception.Message);
}
When you're alone in the Dark, Fear will protect you...
|
|
|
|
|
The best (and pretty much only) way I have ever found to address this kind of problem is to hook into the EditingControlShowing event of the DataGridView. When you do, you can detect that it is a ComboBoxCell being shown, and that the control is a DataGridViewComboBoxEditingControl (or something to that effect). This control is a very simple child class of the normal ComboBox control with a few extra properties for databinding in a data grid.
In the EditingControlShowing event, you can hook into the ComboBox as you normally would, and detect when its data changes and when it is closing. When it is closing, you can edit the data being bound to the ComboBox's available items list.
One caveat, I've never tried this. It may be impossible - I don't know exactly what order the DataGridView would carry out this operation (control closes->you add new item to bound data list->DataGridView validates the input against this new list). It may be that the data is validated before it will change the list of data, in which case you're up a creek unless you can find some way to add new items as the user is typing.
At any rate, if this is possible at all, you'll probably find some way to do it using this event.
There may be another way, and I'm all ears if there is - these are the kinds of problems I love solving with the DGV
|
|
|
|
|
Hi.
I have one form and three radiobuttons. Also, there are four numericUpDowns (two pairs of one for hours and one for minutes).
When a user clicks on one of the radiob. the action should change the minimum and maximum values of all the numUpDowns.
Well, the problem is - Only one works and only once.
The code is the same for all of them, only the numbers change :
private void rDop_CheckedChanged(object sender, EventArgs e)
{
// Setting the default limits for the arrival time of the selected work shifts
timeStartH.Maximum = 15; // end hour of shift.
timeStartH.Minimum = 7; // minimum arrival of shift.
timeEndH.Minimum = 7; // minimum end of shift.
timeEndH.Maximum = 15; // maximum hour of shift.
}
Ive tried with Click () action on radios but it does the same thing (logical).
Any ideas?
Thanks infront.
Matjaž
Força Barça!
|
|
|
|
|
What about the following :
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (radioButton1.Checked)
{
MessageBox.Show("Radio1");
}
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
if (radioButton2.Checked)
{
MessageBox.Show("Radio2");
}
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
if (radioButton3.Checked)
{
MessageBox.Show("Radio3");
}
}
|
|
|
|
|
Thanks. Also tried this before but it cames the same. Maybe the problem is in valuechangers?
Força Barça!
|
|
|
|
|
Well, the problem is - Only one works and only once
Is this the radio button or the numeric up down..?
|
|
|
|
|
hmm i mistyped. it works only once for the change of numUpDown.
Força Barça!
|
|
|
|
|
Ok this works.... maybe i am misunderstanding your question...?
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (radioButton1.Checked)
{
numericUpDown1.ResetText();
numericUpDown1.Maximum = 15;
numericUpDown1.Minimum = 7;
numericUpDown2.ResetText();
numericUpDown2.Maximum = 15;
numericUpDown2.Minimum = 7;
}
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
if (radioButton2.Checked)
{
numericUpDown1.ResetText();
numericUpDown1.Maximum = 10;
numericUpDown1.Minimum = 5;
numericUpDown2.ResetText();
numericUpDown2.Maximum = 10;
numericUpDown2.Minimum = 5;
}
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
if (radioButton3.Checked)
{
numericUpDown1.ResetText();
numericUpDown1.Maximum = 20;
numericUpDown1.Minimum = 3;
numericUpDown1.ResetText();
numericUpDown2.Maximum = 20;
numericUpDown2.Minimum = 3;
}
}
|
|
|
|
|
Try this:
Assume you have three RadioButtons, rb1, rb2 and rb3.
void RadioButtonChanged(object sender)
{
RadioButton rb = sender as RadioButton;
if (rb == rb1 && rb.Checked)
else if (rb == rb2 && rb.Checked)
else if (rb == rb3 && rb.Checked)
}
And call this in your RadioButtons CheckedChanged event.
When you're alone in the Dark, Fear will protect you...
|
|
|
|
|
Thanks to you all for your anwsers and for taking the time to help me with my problem, which is now solved.
Thanks again
Matjaž
Força Barça!
|
|
|
|
|
i want to get network systems hardware information whit a c# application!
i have IP and administration username and passwod.i want install this application on server.
please help me !
ali.moharrami@gmail.com
|
|
|
|
|
Hi Ali,
Welcome to CP
moharrami wrote: i want to get network systems hardware information whit a c# application!
[Edit]
A simple Google search could help you faster.
[\Edit]
There's a great article in CP, titled "How To: (Almost) Everything In WMI via C#", by thund3rstruck. I think the 3rd part[^] can help you.
Good Luck
When you're alone in the Dark, Fear will protect you...
|
|
|
|
|
how to read the prevoius row using a sqldatareader(rs) object(opposite of rs.next())
|
|
|
|
|
You can't move backwards in the data stream. Once you move forward, the previous record doesn't exist any more.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
thanx. then how can i get the previous record , what is the option i can use ..
|
|
|
|
|
Don't use sqldatareader. Use dataset instead.
Apurva Kaushal
|
|
|
|
|
Well, you can't actually use a DataSet instead of a DataReader, as you need a DataReader to populate the DataSet with data from the database.
Also, if the DataReader reads from a large result, it might not be practically possible to put all the data in a DataSet.
Back to prasadbuddhika about what it is that he actually is trying to do...
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
You need to save the current record, then when you read next the record the one you have saved is the prevous record. Obvious really!
Bob
Ashfield Consultants Ltd
|
|
|
|
|
That's true if you only need to back up one record. Suppose you want to back up two....
The previous answer is, I think, more what the questioner needed.
If you want to move forward AND backward, you need to use a DataTable, not a DataReader.
|
|
|
|
|
David Fleming wrote: That's true if you only need to back up one record.
Which is what he asked
The possibilities are endless - datatables, collections, arrays. It all depends on what he wants to do - and as usual there was no clue to that.
Bob
Ashfield Consultants Ltd
|
|
|
|
|
Instead of asking about what you think is the solution, I think that you should rather ask about what you are trying to accomplish.
Why would you need the previous record? If you are trying to compare some data, perhaps that would be much faster in the database query than in the C# code?
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
what i need is to read previous data to compare data with previous data.
|
|
|
|
|
You can keep the data in a generic List which can be enumerated later for checking. Assume you are getting numbers from data reader.
List<int> data = new List<int>();
using(SqlDataReader reader = getTheReader()){
if(reader.HasRows)
{
int ordinal = reader.GetOrdinal("YourColumnName");
while(reader.Read())
data.Add(reader.GetInt32());
}
reader.Close();
}
|
|
|
|