Listbox: Event SelectedIndexChanged called when add one item - c#

I have this code behind a winforms which simply has a listbox as its only control:
private void Form1_Load(object sender, EventArgs e)
{
listBox1.DataSource = dtv;
listBox1.DisplayMember = "Name";
listBox1.ValueMember = "IDName";
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
DataSet dss = UseDatabase.FillDataSet("Select * From Table Where IDName=" + listBox1.SelectedValue);
string st = dss.Tables[0].Rows[0][0].ToString();
MessageBox.Show(st);
}
Run it say: "Additional information: There is no row at position 0."
Debug I see Event SelectedIndexChanged called when add one item.
Why user have not select item, that event is called
And how to fix this?

As far as I know the only way for you to fix this is by checking that SelectedIndex != -1 before doing anything else on the event handler method.

Related

Calling a method when a listview item is focused C#

I am making a small C# application where the user enters information. the information is stored in an object and the object is in turn stored in a list. The information is displayed to the user in a listview.
I want to make it so that when the user clicks on an item in the listview the index of that item is passed to the list which finds the object with the same index and gets its information. The information is then shown in the same textboxes that the user enters his or hers information in.
My problem is that i do not now what method to call when the user selects a row in the listview.
This is what i have:
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count == 1)
{
index = listView1.FocusedItem.Index;
textBox1.Text = manager.FocusedContact(index).FirstName;
textBox2.Text = manager.FocusedContact(index).LastName;
textBox3.Text = manager.FocusedContact(index).Street;
textBox4.Text = manager.FocusedContact(index).City;
textBox5.Text = manager.FocusedContact(index).ZipCode;
}
}
i tried:
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
textbox1.Text = "hi";
}
so i know that private void listView1_SelectedIndexChanged is the wrong method, or is there some option for the listview that i forgot to toggle on or off?
You should be able to retrieve the selected index like this:
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
var index = listView1.SelectedIndex;
}
If the event is not firing at all, check that the event handler is registered correctly in the form's Designer.cs file. In your case, it should look like this:
this.listView1.SelectedIndexChanged += new System.EventHandler(this.listView1_SelectedIndexChanged);

Sometime Except Combo Box's SelectedIndexChanged

I have one Combo box and it has SelectedIndexChanged Event but i want Ignore that event in some case how can i achieve that functionality.
describe code is below
private void Form1_Load(object sender, EventArgs e)
{
List<string> lstString = new List<string>();
lstString.Add("One");
lstString.Add("Two");
lstString.Add("Three");
foreach (string str in lstString)
cBox.Items.Add(str);
//Here I want Ignore cbox_SelectedIndexChanged Event
cBox.SelectedIndex = 0;
}
private void cBox_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show("Your Selected Item is :- " + cBox.SelectedItem.ToString());
}
You can choose either of the 2 approaches.
Have a bool flag which will be set for those conditions when you want to ignore the event handler from running. And use that flag inside your SelectedIndexChanged method
Subscribe to the event only after you have set cBox.SelectedIndex=0 if that is the only case.
Instead of subscribing the event within the designer (I expect you to do this at the moment) you can subscribe to the event in code after the initialization is done.
private void Form1_Load(object sender, EventArgs e)
{
// Init stuff
cBox.SelectedIndex = 0;
// Event subscription
cBox.SelectedIndexChanged += cBox_SelectedIndexChanged;
}
private void cBox_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show("Your Selected Item is :- " + cBox.SelectedItem.ToString());
}

cannot find where event is being raised from

I Create a function NewLoad() and call it in butto1_click.
And i have event listBox1_SelectedIndexChanged which called itself during operation function "NewLoad"
private void button1_Click(object sender, EventArgs e)
{
NewLoad();
}
private void NewLoad()
{
String text = textBox1.Text.Trim();
textBox1.Text = text;
oleDbSelectCommand1.Parameters[0].Value = text;
dataSet11.Clear(); <<<--- call listbox1_SelectedIndexChanged
oleDbDataAdapter1.Fill(dataSet11);
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
dataSet21.Clear();
}
why this happens and how i can to avoid it?
My psychic debugging skills tell me that the listbox is databound to the dataset.
When you clear your dataset, the listbox is emptied, and the selection changes.
This raises the relevant event.
If you have something selected in list box 1, when you clear it, the selected index will change, thus raising selection changed event.

No events fire when I populate ComboBox programmatically

To make it as simple as possible: ComboBox1 is bound to an empty list (in Form1 load event handler), and there is an event handler associated with ComboBox1:
private void CB1_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show("Event fired");
}
private void Form1_Load(object sender, EventArgs e)
{
CB1.DataSource = list1;
CB1.ValueMember = "Name";
CB1.DisplayMember = "Name";
}
The form is loaded, CB1.SelectedIndex = -1, CB1.Text = "", CB1.Items.Count = 0
When I click on Button1, list1 is populated. Now the situation is as follows:
CB1.SelectedIndex = 0, CB1.Text = "Some Text", CB1.Items.Count =196
BUT, the event didn't fire, although SelectedIndex changed from -1 to 0, and I didn't get MessageBox.Show("Event fired"). However, when the user selects some item from the list, the event fires. Also, there is another button that clears list1, and consequently CB1.Items. When this button is pressed, the event also fires (SelectedIndex changes from X to -1).
I've tried to use other events, such as SelectedValueChanged, TextChanged, SelectionChangeCommitted, with no success.
Although there is a simple brute force workaround for this problem, I still do not understand why the problem appears in the first place, and thus cannot anticipate similar situations. That's why I would be grateful if somebody explained to me why no events are firing in the situation I described.
My comment got enough attention, so it seemed appropriate that I should put this as a potential answer. You should make sure that you've actually assigned the event to the method either through a delegate or in the designer with the properties of the combobox itself.
// Somewhere in the form load or init events
CB1.SelectedIndexChanged += new EventHandler(CB1_SelectedIndexChanged);
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.button1.Click += new System.EventHandler(this.comboBox1_SelectionChangeCommitted);
}
private void Form1_Load(object sender, EventArgs e)
{
List<string> list = new List<string> { "a", "b", "c" };
comboBox1.DataSource = list;
comboBox1.SelectedIndex = 0;
}
private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
{
MessageBox.Show(comboBox1.SelectedValue.ToString());
}
private void button1_Click(object sender, EventArgs e)
{
comboBox1.SelectedIndex = 1;
}
}

Getting data from selected datagridview row and which event?

I have a DataGridView (Selectionmode: FullRowSelect) on a windows form along with some textboxes, so what i want to do is that whenever a user selects a row(click or double_click maybe), the contents of that row must be displayed in the text boxes,
I tried out this code:
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
MessageBox.Show("CEll Double_Click event calls");
int rowIndex = e.RowIndex;
DataGridViewRow row = dataGridView1.Rows[rowIndex];
textBox5.Text = row.Cells[1].Value;
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
int rowIndex = e.RowIndex;
DataGridViewRow row = dataGridView1.Rows[rowIndex];
textBox5.Text = dataGridView1.Rows[1].Cells[1].Value.ToString();// row.Cells[1].Value;
}
there are many other textboxes, but the main problem is that none of the event seems to be triggered, what event should i use to do so, or is there some property of datagrid that i might have set wrong?
Any help would be appreciated...:(
You can use SelectionChanged event since you are using FullRowSelect selection mode. Than inside the handler you can access SelectedRows property and get data from it. Example:
private void dataGridView_SelectionChanged(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGridView.SelectedRows)
{
string value1 = row.Cells[0].Value.ToString();
string value2 = row.Cells[1].Value.ToString();
//...
}
}
You can also walk through the column collection instead of typing indexes...
You can try this click event
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
Eid_txt.Text = row.Cells["Employee ID"].Value.ToString();
Name_txt.Text = row.Cells["First Name"].Value.ToString();
Surname_txt.Text = row.Cells["Last Name"].Value.ToString();
First take a label.
set its visibility to false, then on the DataGridView_CellClick event write this
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
label.Text=dataGridView1.Rows[e.RowIndex].Cells["Your Coloumn name"].Value.ToString();
// then perform your select statement according to that label.
}
//try it it might work for you
You should check your designer file. Open Form1.Designer.cs and
find this line:
windows Form Designer Generated Code.
Expand this and you will see a lot of code. So check Whether this line is there inside datagridview1 controls if not place it.
this.dataGridView1.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellClick);
I hope it helps.
Simple solution would be as below. This is improvement of solution from vale.
private void dgMapTable_SelectionChanged(object sender, EventArgs e)
{
int active_map=0;
if(dgMapTable.SelectedRows.Count>0)
active_map = dgMapTable.SelectedRows[0].Index;
// User code if required Process_ROW(active_map);
}
Note for other reader, for above code to work FullRowSelect selection mode for datagridview should be used. You may extend this to give message if more than two rows selected.
You can use the SelectionChanged Event.
CurrentRow.DataBoundItem will give the bound item.
SelectionMode property should be full row select.
var item = ([CastToBindedItem])dataGridLocations.CurrentRow.DataBoundItem;
tbxEditLocation.Text = item.Name;

Categories