Datagrid Selection Changed Method - c#

Currently I am trying to write a method to let user click on any datagrid cell or row in the datagrid view and after clicking information from datagrid view appers in the various textboxes / combo boxes. The thing is after clicking on the item it just doesn't work. Last time I when I wrote something else it was datagrid multiselect option which set to ture and caused problems, this time I have no idea what I did wrong. Method itself:
private void dataGridView3_SelectionChanged(object sender, EventArgs e)
{
foreach (DataGridViewRow row in this.dataGridView3.SelectedRows)
{
Group selectedGrp = row.DataBoundItem as Group;
if (selectedGrp != null)
{
this.textGrpID.Text = selectedGrp.GrpID;
this.textGrpName.Text = selectedGrp.GrpName;
this.comboBoxGrp.SelectedValue = selectedStd.StdGrp;
}
}
}

You want to show the textboxs for enter the the free text while clicking the row on datagrid. do u use the Edit template for showing the textbox and dropdown?

Why use the SelectionChanged event then? You didn't mention anything about changing selections... you said the user would click on a cell to see this information... so why not handle the Click event instead?

Related

Is there a way to remove the selection line(highlighted) in a databound combox on a datagridview?

I am having trouble with a databound datagridviewcomboboxcell
I want to remove the blue selection line that appears on a databound comboboxcell.
I noticed that if a comboboxcell is not databound but has a collection of items, the blue line does not appear. however a databound combobox does have it.
item collection
Databound
You will see in the first picture there is no blue selection line however in the next picture(databound comboboxcell) there is...
I need to take this selection line away so that when the databound comboboxcell has only one row of data, a user can through only keyboard inputs can make a selection.
I initially tried to add a keyDown event to set the Items[index] which did change the value, however, when i move off the cell, it displays the Model Name and namespace. Then when going back on to the cell it displays the value.
I used the following code to do this:
I added a keydown event to the combobox, and here is the keydown event
private void dataGridView_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (dataGridView1.Columns[dataGridView1.CurrentCell.ColumnIndex].CellType.Name == "DataGridViewComboBoxCell" && dataGridView1.CurrentCell.ReadOnly == false)
{
DataGridViewRow row = dataGridView1.CurrentRow;
try
{
if ((row.Cells[dataGridView1.CurrentCell.ColumnIndex] as DataGridViewComboBoxCell).Items.Count == 1)
{
(dataGridView1.CurrentRow.Cells[dataGridView1.CurrentCell.ColumnIndex] as DataGridViewComboBoxCell).Value = taxcodes[0];
(dataGridView1.CurrentRow.Cells[dataGridView1.CurrentCell.ColumnIndex] as DataGridViewComboBoxCell).DisplayMember = "FullDescription";
(dataGridView1.CurrentRow.Cells[dataGridView1.CurrentCell.ColumnIndex] as DataGridViewComboBoxCell).ValueMember = "TaxID";
}
}
catch
{
}
}
}
Now that I have attempted to change set the value of the combobox without luck, I am on to the next possible solution i can maybe get to work.
If i can make the Combobox have a no selection line when it opnes initially then as soon as that line moves on to the only item in the list it will select give the ability to select that as the value.
NOTE: a databound comboboxcell with more than one items, works well
NOTE: a non-databound comboboxcell with items defined works well, however i need a displaymember as well as value member
The ideal outcome from this query will give me the ability to select a databound combobox item(making use of the ENTER key) when the combobox has only one item.
Final NOTE: when i use the mouse to make the selection on the databound comboboxcell with ony 1 item, it works perfectly.
Thanks for the help if anyone can help
So after consulting a friend, I managed to override the .ToString() method
public override string ToString()
{
return FullDescription;
}
This worked for me in this case and the Keydown route was the correct route to go.

How do I edit a button in a grid with only knowing the Grid Row & Column?

I have a grid with some buttons. And basically want to edit the IsEnabled state of a Button in a specific Grid Row + Column. I don't get how to do this e.g.
public void Disablebutton (int Column, int Row)
{
//disable the button at Grid Row = Row and Grid Column = Column
}
Can someone provide an example how to solve given problem?
EDIT:
I found a temporary solution... Well, it's working, but it's very bad
if("B" + x.ToString()+ y.ToString() == "B00")
{
B00.IsEnabled = false; //B00 is the button name f.e.
}
I don't know if you're using WPF, Windows Forms or anything else, but you should have a Cell in the datagrid that you access using the row and col values. That cell has the button control that you can cast to a variable to change its properties. Something like this:
Button button = (Button)Grid.Cell[Column][Row].Control;
button.IsEnabled = false;
or
Button button = Grid.Cell[Column][Row].Control as Button
button.IsEnabled = false;
Keep in mind that this is a very rough example, since I don't know what technology you're using to paint the interface.

Using mouse click event to get ListViewItem text

I want to use listview to populate it with data and then use mouseclick event to fill some textboxes with data. I looked up an example in msdn:
ListViewItem theClickedOne = listView1.GetItemAt(e.X, e.Y);
ListViewItem theClickedtwo = listView1.FocusedItem;
if (theClickedOne != null)
{
MessageBox.Show(theClickedtwo.ToString());
//do your thing here.
//there is a reference to the listview item we clicked on
//in our theClickedOne variable.
}
but I couldn't think about a way to use it in order to differentiate the listviewitems I use since the fist Column in my program is the same and it will only give me a string with it's name(first Column).I want to have something similar to next example but for treeview.
void treeView1_NodeMouseClick(Object sender, TreeNodeMouseClickEventArgs e)
{
MessageBox.Show(e.Node.Text);
}
When populating you ListView, set the Tag property of the items, e.g.
newItem.Tag = "Item 1";
The Tag property has type object, so you can use anything you want here to identify the item. When handling the mouse click event simply check the Tag value again:
if((string)(clickedItem.Tag) == "Item 1")
{
// do stuff for this specific item.
}

Move Focus to next Control in WPF

Suppose I have a DataGrid and a Button. CanUserAddRows is set to True. Let this dataGrid have two DataGridTextColumns namely "Name" and "Age".
Now if user takes the following actions :
He adds name as XYZ and Age as 20. Then he press enter. So a new row will be added to the datagrid.
He adds name as ABC and Age as 12. Then he press enter. So a new row will be added to the datagrid.
He keeps name empty and press Enter or TAB then I want to move focus to Button instead of next cell of datagrid.
I have watched many questions but I don't get the logic of if user left the name empty and how do I move focus to Button instead of next cell.
Use DataGridView.SelectedCells[0] so you can retrieve the value of the selected cell (assuming you can only select one).
To get the actual string inside, you will have to cast the content to a proper WPF object, like TextBlock.
myCell.Column.GetCellContent(cell.Item) as TextBlock
Then in a PreviewKeyDown event handler (KeyDown having known issues in DataGridView), you can use button.Focus(). (more about those issues)
//...
myDataGrid1.PreviewKeyDown += myDataGrid1_KeyDown;
//...
void myDataGrid1_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == System.Windows.Input.Key.Enter)
{
var cell = myDataGrid1.SelectedCells[0];
TextBlock cellContent = cell.Column.GetCellContent(cell.Item) as TextBlock;
if (cellContent != null)
{
if (String.IsNullOrWhitespace(cellContent.Text))
button.Focus();
}
}
}
About getting the column's name, it's another question, for which you can find answer here for example.
As a side note, you're not really supposed to interact directly with a DataGridView cells' values, since it's meant to be bound with a data source from which you should retrieve the data you want to test. However, you can search a bit for helper methods that can help you get what you want.
You can define a handler for the DataGrid.KeyDown event, as:
void myDataGrid1_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == System.Windows.Input.Key.Enter)
{
button.Focus();
}
}

ObjectListView - Delete a row by clicking on a designated column with fixed content/text

I have a simple question which i am not able to solve myself.
I have an ObjectListView filled with some of my objects. But in addition to that I want to have another column, with a default text "Delete". On clicking that column, the selected Row should be deleted. How do I do that?
You can achieve this by making the desired row editable and use the CellEditActivation event. Initialize your OLV and "delete-column" as follows:
// fire cell edit event on single click
objectListView1.CellEditActivation = ObjectListView.CellEditActivateMode.SingleClick;
objectListView1.CellEditStarting += ObjectListView1OnCellEditStarting;
// enable cell edit and always set cell text to "Delete"
deleteColumn.IsEditable = true;
deleteColumn.AspectGetter = delegate {
return "Delete";
};
Then you can remove the row in the CellEditStarting handler as soon as the column is clicked:
private void ObjectListView1OnCellEditStarting(object sender, CellEditEventArgs e) {
// special cell edit handling for our delete-row
if (e.Column == deleteColumn) {
e.Cancel = true; // we don't want to edit anything
objectListView1.RemoveObject(e.RowObject); // remove object
}
}
To improve on this, you can display an image in addition to the text.
// assign an ImageList containing at least one image to SmallImageList
objectListView1.SmallImageList = imageList1;
// always display image from index 0 as default image for deleteColumn
deleteColumn.ImageGetter = delegate {
return 0;
};
Result:
If you don't want to display any text next to the image you can use
deleteColumn.AspectToStringConverter = delegate {
return String.Empty;
};
You could also set the Aspect to an empty string, but consider this as "best practice". By still returning an aspect, sorting and grouping will still work.
If the "Delete" column is not the first column in the ObjectListView, you will have to set
ShowImagesOnSubItems = true;
See also ObjectListView show icons.

Categories

Resources