this is event of dropdown when a value select from dropdown it will run(Input string was not in a correct format. ERROR always COMES Plz Help ME Now)
private void cmbRoom_SelectedIndexChanged(object sender, EventArgs e)
{
if (cmbRoom.SelectedValue.ToString() != "")
{
int RoomSelectedID = Convert.ToInt32(cmbRoom.SelectedValue.ToString());
BindDataRoomBeed(RoomSelectedID);
cmbBed.SelectedIndex = 0;
}
}
private void cmbRoom_SelectedIndexChanged(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(cmbRoom.SelectedValue.ToString()))
{
int RoomSelectedID = Convert.ToInt32(cmbRoom.SelectedValue.ToString());
BindDataRoomBeed(RoomSelectedID);
cmbBed.SelectedIndex = 0;
}
}
Hope your value is integer only..
What is the type of collection items binded to your control?
This type overrides ToString method?
Does overloaded ToString method of this type returns you number?
I think the reason is in not overloaded ToString method of collection item type.
// try this
int RoomSelectedID = Convert.ToInt32(cmbRoom.SelectedText.ToString());
Related
here is the code, please help. Its not working. I was promised it would.
I cant convert the string of the text box to an int so i cant do the math required.
public Form1()
{
InitializeComponent();
}
int userVal = int.Parse(Form1.textBox1.Text);
private void button1_Click(object sender, EventArgs e)
{
int answer = (Form1.textBox1 * Form1.textBox2);
MessageBox.Show("MPG: ", answer);
}
First of all. You should really start at the basics, because in your code you try to multiply two TextBox controls which isn't possible.
Secondly. I corrected your code.
Int32.TryParse(someString,out anInt) tries to convert the first parameter(someString) to an Int32 and returns a boolean value about the conversion whether it was successful or not. If the conversion was successful the converted value is stored in the second parameter(anInt) and Int32.TryParse(someString,out anInt) returns true.
In the corrected code you simply try to convert the strings from both TextBoxes. If you were able to do so (return value of Int32.TryParse) simply multiply both ints you got from Int32.TryParse
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int num1,num2;
If(Int32.TryParse(textBox1.Text,out num1) && Int32.TryParse(textBox2.Text,out num2))
{
int answer = num1 * num2;
string output = "MPG: "+ answer.ToString();
MessageBox.Show(output);
}
}
int userVal = int.Parse(Form1.textBox1.Text);
First of all, this line must be placed in the button1_Click function in order to work. But now the issue is that you're using a string input to accept numbers, which is not a good idea because if you type "abc" for example, int.Parse will throw an exception.
Now you just have to replace textBox1 and 2 to numerical inputs, and then use this code:
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int answer = (numericalInput1.value * numbericalInput2.value);
MessageBox.Show("MPG: ", answer);
}
Im trying to get an int[] array but it's returning an error which is Value cannot be null. I'm still a newbie to c#. Can someone help or give a clue how to solve this? I tried many techniques of passing the value but still no luck.
here's where the array came from
private void btnTag_Click(object sender, EventArgs e)
{
List<int> employee_id_list = new List<int>();
foreach (Control c in panelEmployee.Controls)
{
if (c is CheckBox)
{
if (((CheckBox)c).Checked)
{
employee_id_list.Add(Convert.ToInt32(c.Tag));
}
}
}
var type = Type.GetType("Payroll." + dynamic_form);
dynamic form = Activator.CreateInstance(type) as Form;
form.GetEmployeeID(employee_id_list.ToArray());
this.Close();
}
here's where i displayed it
public int[] emp_id;
public void GetEmployeeID(int[] employee_id)
{
emp_id = employee_id;
//in this code there are no errors and it is showing the array
MessageBox.Show(string.Join(Environment.NewLine, emp_id));
}
private void btnSave_Click(object sender, EventArgs e)
{
// but when i trigger this it returns and error Value cannot be null
MessageBox.Show(string.Join(Environment.NewLine, emp_id));
}
I think your problem is that you lose the value of the variable emp_id between the GetEmployee call (it assigns value to emp_id from the parameter so it will work always you pass a not null value) and the later call to btnSave_Clickthat is trying to use the previously( and hopefully) assigned value of emp_id
Maybe you are using WebForms , in that case you should assign the emp_id to a Session state or something that won't get deleted on next request.
public int[] emp_id;
public void GetEmployeeID(int[] employee_id)
{
emp_id = employee_id;
//in this code there are no errors and it is showing the array
MessageBox.Show(string.Join(Environment.NewLine, emp_id));
}
private void btnSave_Click(object sender, EventArgs e)
{
// but when i trigger this it returns and error Value cannot be null
MessageBox.Show(string.Join(Environment.NewLine, emp_id));
}
I'm trying to implement DeleteItem functionality on my DataGridView.
I have the following event:
private void btnDeleteDjelatnik_Click(object sender, EventArgs e)
{
int idDjelatnik = -1;
int index = djelatnikDataGrid.CurrentRow.Index;
Int32.TryParse(djelatnikDataGrid.Rows[index].Cells[0].Value.ToString(), out idDjelatnik);
r.DeleteDjelatnik(idDjelatnik);
}
I'm trying to get selected rows ID Column so I can pass it to my Delete method: DeleteDjelatnik(int slectedID);
the following line always gives me value of 3:
int index = djelatnikDataGrid.CurrentRow.Index;
I also tried
int index = djelatnikDataGrid.SelectedRows[0].Index;
but I'm getting ArgumentOutOfRange exception, yes my SelectionMode is on FullRowSelect
How to get this to work?
Use DataGridView's UserDeletingRow event,
private void dataGridView1_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
{
int deletingRowIndex = e.Row.Index;
}
Hope helps,
Since DataGridView Index property doesn't return correct value I wrote this piece of code:
public int djelatnikSelectedRowIndex { get; set; }
private void djelatnikDataGrid_CellClick(object sender, DataGridViewCellEventArgs e)
{
djelatnikSelectedRowIndex = e.RowIndex;
}
The real problem was DeleteItem property in my BindingNavigator!
Before I set it to "None" it would delete the row from my DataGridView before the execution of "btnDeleteDjelatnik_Click" and mess up the indexation so I couldn't get the real ID of the row I wanted to delete!
My method now looks like this:
private void btnDeleteDjelatnik_Click(object sender, EventArgs e)
{
int index = djelatnikDataGrid.SelectedRows[0].Index;
int idDjelatnik = -1;
Int32.TryParse(djelatnikDataGrid["IDDjelatnik", index].Value.ToString(), out idDjelatnik);
r.DeleteDjelatnik(idDjelatnik);
djelatnikDataGrid.Rows.RemoveAt(index);
}
Thanks for help everyone!
Can I make multiple filter in gridview? So far I have three separate methods for each kind of filtering. I want to be able to do something like this. First choose from combobox value which will be shown and then from this filtered list, I would like to be able to search by using textbox for something else.
private void button9_Click(object sender, EventArgs e)
{
var result = list3.Where(Srodek => Srodek.Srodek.ID.Device == textBox2.Text).ToList();
dataGridView4.DataSource = result;
}
private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
{
var result = list3.Where(Srodek => Srodek.Srodek.category1 == comboBox1.SelectedItem.ToString()).ToList();
dataGridView4.DataSource = result;
}
Now when I choose some value from combobox it show what I want in gridview, but later when I insert something in textbox and click the button, it is filtering the whole list not this one already filtered by combobox. How can I achieve it?
Try FilterDataGrid() calling on both events:
private void button9_Click(object sender, EventArgs e)
{
FilterDataGrid();
}
private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
{
FilterDataGrid();
}
private void FilterDataGrid()
{
var _text = Convert.ToString(textBox2.Text);
var _comboText = ! string.IsNullOrEmpty(comboBox1.Text) ? Convert.ToString(comboBox1.SelectedItem) : string.Empty;
var result = list3.Where(Srodek => Srodek.Srodek.category1 == _comboText || Srodek.Srodek.ID.Device == _text).ToList();
//
dataGridView4.DataSource = result;
}
Hopes this helps u.
I suppose the type of element in list3 is T:
private void button9_Click(object sender, EventArgs e)
{
if(dataGridView4.DataSource is IEnumerable<T>){
var result = ((IEnumerable<T>)dataGridView4.DataSource).Where(Srodek => Srodek.Srodek.ID.Device == textBox2.Text).ToList();
dataGridView4.DataSource = result;
}
}
Make a global property that will host your list so that everytime you perform a query on the list you perform a query on your up to date list that has already been filtered.
You aren't changing the source of you filter, so you are filtering on the same set each time. One option would be to have the original dataset and a filtered dataset, then when you filter the original save the results in the filtered set.
I want to create a if statement that recognizes which string has been removed from a specific list box. I thought i could do an if statement similar to the one below and get it to work but it tells me it has invalid arguements - if anyone can guide me it would appreciated
private void button2_Click(object sender, EventArgs e)
{
listBox2.Items.RemoveAt(listBox2.SelectedIndex);
if(listBox2.Items.RemoveAt(listBox2.SelectedItems.ToString().Equals("Test")))
{
picturebox.Image = null;
}
}
You need to check the SelectedItem before removing it:
private void button2_Click(object sender, EventArgs e)
{
if (listBox2.SelectedIndex != -1)
{
if (listBox2.SelectedItem.ToString().Equals("Test")))
picturebox.Image = null;
listBox2.Items.RemoveAt(listBox2.SelectedIndex);
}
}
I’ve also added a check to ensure that an item is actually selected (since you would get errors otherwise).
Your issue is that you are calling ListBox.Items.RemoveAt(int index) and passing in a boolean value: listBox2.SelectedItems.ToString().Equals("Test")).
In addition, you're removing the item first, and then calling RemoveAt again, which will actually remove a different item (whatever is now at that index), or throw an exception if you've gone outside of the bounds of your ListBox collection.
You should first check if your selected item equals "Test", and then remove the item from your ListBox, like so:
private void button2_Click(object sender, EventArgs e)
{
// SelectedIndex returns -1 if nothing is selected
if(listBox2.SelectedIndex != -1)
{
if( listBox2.SelectedItem.ToString().Equals("Test") )
{
picturebox.Image = null;
}
listBox2.Items.RemoveAt(listBox2.SelectedIndex);
}
}
You should do something like :
String deletedString = listBox2.Items.ElementAt(listBox2.SelectedIndex).ToString();
listBox2.Items.RemoveAt(listBox2.SelectedIndex);
if(listBox2.Items.RemoveAt(deletedString == "Test"))
{
picturebox.Image = null;
}
It might not compile (Check if Items has SelectedItem property).