How to get an info from DGV when mouse is on? - windows

If I have a datagridview and I have a button and I want that the moment I press this button, I'll get string from the column the mouse is on.

For getting a current selected cell from datagridview("marked cell"):
mydatagridview.CurrentCell
and get a value of this cell
mydatagridview.CurrentCell.Value
For be sure: check if CurrentCell exists(C#)
if(mydatagridview.CurrentCell != null)
String selectedValue = mydatagridview.CurrentCell.Value.Tostring();
MSDN DataGridView documentation

Related

Delete Record VB2010 w/ TableAdapter

This shouldn't be this hard... But it's late.
I am working on a simple form, and trying to delete a record from a connected DataSource while using a TableAdapter. Here is the SQL for the TableAdapter;
DELETE FROM Main WHERE (ID = ?) AND (tbl_Job_Name = ?)
Main is the table name, only two fields.
I am populating a ComboBox with this data, and I am using a Button to call the Delete() action like this;
Private Sub btnDeleteJob_Click(sender As Object, e As System.EventArgs) Handles btnDeleteJob.Click
Dim deleteJobAdapter As New DCGDataSetTableAdapters.MainTableAdapter
deleteJobAdapter.DeleteQuery(ComboBox2.SelectedIndex, ComboBox2.SelectedText)
End Sub
When I break the code I can see the ID value, but the SelectedText field is blank, and of course when it runs through the record is not deleted. I would ideally like to just pass the ID of the selected record in the ComboBox to delete the record. What am I missing?
Try,
deleteJobAdapter.DeleteQuery(ComboBox2.SelectedIndex, ComboBox2.Text)
SelectedText property:
Gets or sets the text that is selected in the editable portion of a ComboBox.
Text property:
Gets or sets the text associated with this control.
You can use the SelectedText property to retrieve or change the currently selected text in a ComboBox control. However, you should be aware that the selection can change automatically because of user interaction. For example, if you retrieve the SelectedText value in a button Click event handler, the value will be an empty string. This is because the selection is automatically cleared when the input focus moves from the combo box to the button.
When the combo box loses focus, the selection point moves to the beginning of the text and any selected text becomes unselected. In this case, getting the SelectedText property retrieves an empty string, and setting the SelectedText property adds the specified value to the beginning of the text.
When I enter the code;
deleteJobAdapter.DeleteQuery(ComboBox2.SelectedIndex, ComboBox2.Text)
My break point shows the correct text from the record, but I did notice that the ID value is incorrect, the SelectedIndex returns a sequential number of the record itself, starting with "0". So it looks like SelectedIndex does not return the actual ID value...
And back to the original issue, the record selected is still not deleted.
This is what I ended up using;
Dim delJobID = ComboBox2.SelectedValue
Dim delJobRowAdpt As New DCGDataSetTableAdapters.MainTableAdapter
Dim delJobRow As DCGDataSet.MainRow
Dim intDelete As Integer
delJobRow = DCGDataSet.Main.FindByID(delJobID)
delJobRow.Delete()

RadGrid EditColumn Insert Data to an Edit Field from a RadGrid in a RadWindow

I have a telerik:RadGrid that contains Bound Data,
I am calling Popup Edit Control Of RadGrid, I am getting all the fields and edit works fine.
What I want to do is from the Edit Popup, Edit one of the fields (Which is a RadTextBox) by clicking a button to open a RadWindow, this Window Contains another RadGrid with user details and one of the column with a button that executes RadGrid_OnCommand event, I am passing one of the values of the Grid by:
CommandArguments='<%# Eval("UserName")%>'
How can I place this value to the RadTextBox.Text in the Edit PopUp, So that I can update the grid with the selected value?
I would really appreciate any help. Thank you in Advance
I solved the issue by geting the grid row, which is in edit mode so I got the value of which row I need to change and updated its Editible item by ID using this code:
var rowid = RadGrid1.EditIndexes[RadGrid1.EditIndexes.Count-1];
GridEditFormItem rowEditControls;
foreach (GridDataItem row in RadGrid1.Items)
{
if (row.ItemIndex == int.Parse(rowid))
{
rowEditControls = row.EditFormItem;
((rowEditControls as GridEditableItem).FindControl("ID") as RadTextBox).Text = e.CommandArgument.ToString();
}
}
I Hope this will be helpfull for someone, I find it valuable for customising your edit forms.

LiveCycle drop-down list change event only works on second change

I want a Text Field to appear when a certain item is chosen from a drop-down list. I'm using a change event.
if(this.rawValue == 1){
Tolerance.presence = "visible";
}
else{
Tolerance.presence = "hidden";
}
The problem is that the Text Field presence does not change immediately when a selection is made, but only after I go back to the list box and select again (any value, not just the same one).
The new value of the dropdown only registers after the change event. This means this.rawValue points to the old value of the dropdown in a change event.
Either move your script dropdown exit event or make use of the event.newText in the if conditional in the change event.

How to change the color of an specific cell of Janus GridEX?

I Want to change the foreground and background color of some specific cells dynamically, depending to another cell values or events.
For example, when The user clicks the cell, Its back color should be RED.
My code is This:
Janus.Windows.GridEX.GridEXFormatStyle style1 = new GridEX.FormatStyle();
style1.ForeColor = Color.Red;
mySpecificCell.FormatStyle = style1;
It works, but when I scroll down and then scroll up again, the color of cell returns to original color.
What is the problem with my code? How should I overcome this?
Like Arthur said, you have to utilize the FormattingRow event of the grid.
This is a sample code:
private void grd_FormattingRow(object sender, RowLoadEventArgs e)
{
if (e.Row.Cells["ColumnName"].Value == someValue) // a condition to determine when to change the color of the cell, you can put your own condition
e.Row.Cells["ColumnName"].FormatStyle = new GridEXFormatStyle() { BackColor = Color.Red };
}
The Formatting Row will fire for each row in the grid that is being displayed and you can access this row using e.Row
"ColumnName" is the name of the column.
You can replace the condition t ocheck when you want to change the color of the cell.
Try using the Gridex's formattingRow event to do your customized formatting.
This event is called for every row on the grid.
There you have access to the full row.
That means you could check the value of some cell and then format another cell based on the first.

Passing value from GridView selected value to DropDownList Default Value

How do I pass a selected value from GridView to a DropDownList default value. The DDL always show the first item from the table. I want to be able to display the first item as the value from another GridView (selected Item).
Thank you in advance for your help.
On your DataGridview Event where you get the selected value add.
//YOur Bindinding on Dropdown
drpdown.DataSource = //;
drpdown.DataTextField = "YourTablecolumn";
drpdown.DataBind();
string gvalue = //gridselected value;
drpdown.Items.Insert(0, new ListItem(""+ gvalue +"", "0"));
drpdown.SelectedIndex = 0;
Hope this help.
Regards

Resources