What I have going on is a listview inside of my windows form.
How can I make so that only when you double click a row it pulls data for row X and column 3.
meaning I have a listview of...
A|B|C|D
1|2|3|4
#|#|$|%
Bc|Dv|D#|dg
so if i double clicked row thats begins with # it will read in column 3 ($).
I aleady have FullRowSelect = True
I figured this out thanks everyone!
string hyperurl = listView1.FocusedItem.SubItems[2].Text;
Use this code for your ListView's DoubleClick event:
private void listView1_DoubleClick(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
ListViewItem item = listView1.SelectedItems[0];
MessageBox.Show(item.SubItems[2].ToString());
}
}
Maybe this can help:
http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.selectedindexchanged.aspx
Related
I have a problem. I have three buttons of main categories and when you click one of these buttons some things appear in ListBox and the buttons with subcategories appear. If you click on subcategory, consequently different things appear in ListBox.
I have methods like that:
private void DisplayPeople(string category); //I use it ParentClicked and SubClicked
private void ParentClicked(object sender, EventArgs e); //for parent category
private void SubCatClicked(object sender, EventArgs e); //for subcategory
myListBox.SelectedIndexChanged+= new EventHandler(selectedIndexChange);
When you select sth in listbox then it should appear in my DataGridView and it works perfectly. However, when I click on the button and things appear in listbox, and I put sth like myListBox.ClearSelected(); or my.SelectedItem = null; I see NOTHING is selected at the begining but still SelectedIndexChange event works because it adds first row to my DataGridView. I have no idea why, could u help me?
MUCH MORE SHORTER:
In my program when you select sth in ListBox, it appears in DataGridView. When i set myListBox.ClearSelected(); or my.SelectedItem = null;, nothing is selected in the begining but SelectedIndexChange event works and first thing in listbox is added to DataGridView. I don't want that, I want it to appear in datagridview only when it is selected by the user.
NOW MY EVENT HANDLER LOOKS LIKE THAT:
private void selectedIndexChange(object sender, EventArgs e)
{
Person person = (Person)MyListBox.SelectedItem;
if (MyListBox.Items.Count > 0 && MyListBox.SelectedItems.Count > 0)
{
Basket.Add(person);
dataGridView1.DataSource = Basket;
}
}
PS.
I SOLVED THE PROBLEM. I did it that the thing from listbox is added to DataGridView when user clicks on ListBox and SelectedIndexChange event appears. But maybe there is simpler and prettier solution?
That's exactly how SelectedIndexChanged is supposed to work.
What you have to do is compare myListBox.SelectedIndex to -1 or myListBox.SelectedItem to null to see if something is actually selected in the ListBox.
http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.selectedindexchanged(v=vs.110).aspx
try like this
private void myListBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (myListBox.Items.Count > 0 && myListBox.SelectedItems.Count > 0)
{
//Do something in DatagridView
}
else
{
//clear the gridview
}
}
I have this code in my C# program, but it throws a fit when some buttons are clicked because there is no row selected in the DataGridView (I use the ClearSelection method):
string selectedUser = usersGrid.SelectedRows[0].Cells[1].Value.ToString();
Is there some sort of check I can do before the above line to ensure that a row is selected?
if (usersGrid.SelectedRows.Count > 0)
I am going to take a stab at what I think you are trying to do, try this below
private void myButton_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in usersGrid.Rows)
{
if (this.usersGrid.SelectedRows.Count == 1)
{
// get information of 1st column from the row
string selectedUser = this.usersGrid.SelectedRows[0].Cells[0].ToString();
}
}
}
also do the following as well and checkout the link
Set DataGridView.MultiSelect=false and DataGridView.SelectionMode = FullRowSelect. This will make it so the user can only select a single row at a time.
I want to know how to check whether a gridview's row got selected or not.
I am working on windows application.
I want to put a if condition ie if a particular row gets selected then fill the textbox with the correspoding cell value.
I am just not getting the way how to give the condition in the if clause.
Handle the DataGridView.SelectionChanged event. Use the DataGridView.SelectedRows property to get the selected rows collection.
private void dataGridView_SelectionChanged(object sender, EventArgs e)
{
// Update the text of TextBox controls.
textBox1.Text = dataGridView.SelectedRows[0].Cells[1].Value.ToString();
textBox2.Text = dataGridView.SelectedRows[0].Cells[2].Value.ToString();
....
}
Check DataGridViewRow.Selected property.
if (dataGridView.Rows[rowIndex].Selected)
{
// Do something ..
}
Check the selected property of DataGridViewRow, it returns true for selected else false.
bool isSelected = dataGridView1.Rows[e.RowIndex].Selected;
You can subscribe to the SelectionChanged event of the control and iterate through each selected row if multi-selection is enabled or just the first one if single-row selection only.
private void MyGridView_SelectionChanged(object sender, EventArgs e)
{
for (int i = 0; i < MyGridView.SelectedRows.Count; i++)
{
MyTextBox.Text = MyGridView.SelectedRows[i].Cells[0].Value.ToString(); //assuming column 0 is the cell you're looking for
// do your other stuff
}
}
More information can be found on the SelectedRows property.
Don't ask why, but I need a way to prevent the user from entering a cell in the 'new' datagridview row WHILE they've got multiple rows selected.
Currently, the cell in the first column of the row that the mouse is hovering over during the click and drag is being selected. If you click on the cell, then the rows aren't selected anymore, so you can't use any cell click events or anything.
Any suggestions are welcome.
P.S. don't try editing the currentcell from the selectionchanged event, already tried that!
Thanks,
Isaac
I have an idea that the DataGridView control has a RowState property. When your row is selected, check the state. I'm not sure what the states are (if I'm even in the ballpark) ... anyway, you may be able to check that route.
I'm looking for more info ...
Okay ... think I found a decent way to do this:
void DataGridView1_RowsAdded (object sender, DataGridViewRowsAddedEventArgs e)
{
currentNewRow = e.RowIndex;
}
void DataGridView1_CellMouseClick(Object sender, DataGridViewCellMouseEventArgs e)
{
if (e.RowIndex == currentNewRow)
{
// don't add to Selection
}
}
Use CellStateChanged event:
private void dataGridView_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e) {
if (e.Cell.OwningRow.IsNewRow && dataGridView.SelectedRows.Count > 1)
e.Cell.Selected = false;
}
Hi all i will have some data in my datagrid as follows
Now if i click on first column on any cell i would like to show a from, and if i click on 2nd row on any cell value i would like to show another form. How can i do this...
I got the answer
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
string s = dataGridView1.CurrentCell.RowIndex.ToString();
if (Convert.ToInt32(s) == 0)
{
Form f = new Form();
ActivateMdiChild(f);
f.Show();
}
if (Convert.ToInt32(s) == 1)
{
MessageBox.Show("Hi");
}
}
You need to keep some value identifying form to launch in the data table (or in hidden column). Now, in click event, you can look up for that value within current row and launch the needed form.