DatagridView Highlight Event - WINFORM C# - c#

I have a combobox which is connected to the database so I populate the value of my combobox based on what's in my database. my combobox is another FORM from the datagrid. So here's I want to achieve.
form1 = datagrid (based on the database)
form2 = combobox (based on the database)
I want that If I highlight a certain row (My selection mode = fullrowselect) and press a button the comboBox will automatically point to that row.
for ex.
datagrid
name: Joe (highlighted)
*user clicks the button whch in my case is edit
*load edit form
comboBox.SelectedIndex is = highlighted row (which the user clicks)
I can show you my code if it helps. thanks :))
THANKS! :))

You can try to set in the following ways, you can pass the value Joe to the other form via a parameter in the constructor. This could be then used to select you required value in the ComboBox
comboBox2.SelectedIndex = comboBox2.Items.IndexOf("Joe");
comboBox2.SelectedText = "Three"; // or SelectedValue depending on how you are binding
EDIT
Avoid accessing the grid directly from the other form, expose the value required as a property or better pass it to the new form as parameter.
Joe could be the value of the cell like dataGridView2.CurrentRow[0].FormattedValue and pass this to the new form constructor like new Form2(object datagridvalue). Then use the value in the form later on.

Related

WFA DataGridView Load from Another Form

I have a windows form with DataGridView (Form: DataGridForm) on it and a button for an advanced search which opens a new windows form (SearchForm) with the options of search:
ComboBox1 - Name, Lastname, Age, etc...
Combo Box2 - Equals, Does not equal, Contains, etc...
Based on the selected items of the ComboBoxes either a textbox or datetimepicker control is displayed.
For each selected option, there is a particular TableAdapter query method invoking. For example, if there are selected Name (ComboBox1) and Equals (ComboBox2), the textbox control is displayed which should pass an input to the query method - FillBy_EqualsName().
So, the problem is to load a result of the search to the DataGridView which is displayed on another windows form. For this, I'm using a piece of code in the button_click (Form: SearchForm) event but it does not work 👇
Forms.DataGridForm dataGridForm = new Forms.DataGridForm(); dataGridForm.user_TableTableAdapter.FillBy_CotainsName(dataGridForm.userGridDataSet.User_Table, this.search_textbox.text);
What am I doin wrong?
Thanks in advance.

Modifying Databound ComboBox at run time

High level view of the application is:
Form1 displays client information in a DataGridView (pulled from a DB).
I save the client's information from the DataGridView in properties located in a Client class.
From Form1, the user can click a button that instantiates Form2 and allows the client's information to be modified.
Form2's constructor has a Client object as a parameter. This object is what holds all the client's information from Form1. Using this object i can re-populate the fields that i want the user to be able to edit on Form2.
There is a table in the DataSource that holds all the case types (ie. CaseType1, CaseType2, CaseType3).
I then use a ComboBox and populate it with all the case types from the DataSource when the form is instantiated. My ComboBox settings are as follows:
DataSource: set to my CaseTypeBindingSource which has the columns and data i need.
DisplayMember: The result of the query being used.
ValueMember: The result of the query being used. (same as DisplayMember)
SelectedValue: I have tried it with "none" and with the same value as DisplayMember and ValueMember.
Here's the problem:
At run-time, i want to be able to assign the client's case type that was brought over from Form1 as the item that is currently selected in the ComboBox (SelectedItem).
I could just assign the case type to a textbox and be done with it. But the idea is that i would like the form to show the user what the client's case type is, and allow him/her to change it by using the ComboBox.
The ComboBox name is CaseTypeComboBox. The object holding the case type information and its property is client.CaseType.
I've tried the following inside the constructor:
CaseTypeComboBox.SelectedItem = client.CaseType;
I've also tried creating a class variable in Form2 called origCaseType, assigning the client.CaseType value to the origCaseType class variable in the constructor. Then doing the following when the Form2_Load(...) event fires:
CaseTypeComboBox.SelectedValue = origCaseType;
Does anyone have any thoughts on this? Any similar experience?
Please let me know if anything needs clarification, any help is appreciated.
Thanks in advance!
TS
"Form2" Constructor:
public ModifyCase(Client client)
{
InitializeComponent();
CaseNumberTextBox.Text = client.CaseNumber;
LoadStatusComboBox(client.Status);
LoadIsClosedRadioButton(client.IsClosed);
LoadIsInStorageRadioButton(client.IsInStorage);
LastModifiedTextBox.Text = client.LastModified.ToString();
NotesTextBox.Text = client.Notes;
origCaseType = client.CaseType;
}
"Form2" Load event:
private void ModifyCase_Load(object sender, EventArgs e)
{
//Fills the ComboBox box with case types
this.case_typesTableAdapter1.Fill(this.testDataSet1.case_types);
//Attempts to set value for client's case type from Form1
CaseTypeComboBox.SelectedValue = origCaseType;
//Just to help me see what these variables are holding to figure
//out the problem...
MessageBox.Show(origCaseType);
MessageBox.Show(CaseTypeComboBox.SelectedIndex.ToString());
}
I made two small adjustments that did the trick! The problem was i had set SelectedItem and SelectedValue to the DataSource column i'm using. Except, all the was needed was to set those two values to "none", and set the ValueMember and DisplayMember to the DataSource column. Thanks for your time wdavo!

Open a form based on a row selection from a datagrid

Out of curiosity is it possible to open a form based on row selection in a datagrid? I would also need the form to show information based on the username in the datagrid. The persons username is included within the row of the datagrid.
You will have to code this, but yes, it is possible.
First, populate your DataGrid with data that you can handle.
On the DataGrid's Selection Changed event, read that data, create the form you want to show (if it does not already exist), and display it using Show().
This would be like a typical Menu program.
You can handle this under the following event
dataGridView1_CellClick
Get the CurrentCell value of the datagridiview
Check for the username exists or not as per you asked and show the respective form
Sample code:
if (this.dataGridView1.CurrentCell != null)
{
string strusrname=dataGridView1.CurrentCell.Value.ToString();
//Here find out for the user name from the string as you get the currentcell value of the datagridview
// Raise the corresponding form as per you required
}
Not really sure if this is what your after, as i don't no if you want to show the data on another pre-built form or create a new one, but here it goes.
This way you won't even need to worry about the row selected, assuming you have the username of the person bound to the datagrid you can create a hyperlinkcolumn like this:
<asp:HyperLinkcolumn DataNavigateUrlField="Username"
DataNavigateUrlFormatString="PersonForm.aspx?Username={0}"
HeaderText="More Details"
Text="View Person Details" />
Then the PersonForm can load the persons details. Or if you would like some help on how to catch the selected row on itemcommand then let me no.
Hope this helps.
EDIT: after your winforms tag update you may be interested in this: DataGridViewLink On MSDN
The general code is:
DataGridViewLinkColumn links = new DataGridViewLinkColumn();
links.UseColumnTextForLinkValue = true;
links.HeaderText = ColumnName.ReportsTo.ToString();
links.DataPropertyName = //Set your field here.
links.ActiveLinkColor = Color.White;
links.LinkBehavior = LinkBehavior.SystemDefault;
links.LinkColor = Color.Blue;
links.TrackVisitedState = true;
links.VisitedLinkColor = Color.YellowGreen;
DataGridView1.Columns.Add(links);
Once you have added a link you can catch it using DataGridView1_CellContentClick and do what you want with it, ie open a new form or alter the current one.

WinForms, BindingSource, prevent CurrentItem change

How can I prevent bindingSource Current item from changing?
(there is no changing event with cancel argument...)
This is scenario:
I have a dataGridView, and text-boxes on the same form.
I am using text-boxes to change values in the datasource (with standard databinding)
Bindings are written manually (After save button is clicked)
When user selects another row using DataGridView, bindingSource.Current propery is changed, and text boxes are updated with values from selected row. Changes that user entered are lost.
Is there any way to prevent this problem?
Can I prevent bindingSource.Current property from changing?
Is there any better option to prevent this behaviour?
(disabling dataGridView is unfortinutelly not an option)
It sounds like you don't want to not change bindingSource.Current, but rather you want to save the contents of the text boxes before you change the current row? If you've bound a collection to the bindingSource, then don't the text boxes refer to properties in the current element in that collection?
I'm not really sure what you're trying to do, but a shot in the dark might be to bind the same DataSource to two different BindingSource objects, something like this:
gridBindingSource.DataSource = theDataSource;
textBoxBindingSource.DataSource = theDataSource;
myDataGrid.DataSource = gridBindingSource;
firstNameTextBox.Bindings.Add (new Binding ("Text", textBoxBindingSource, "FirstName"));
but this would be weird, because if theDataSource is appropriate for a grid control, then it's a collection of things that have a FirstName property. Maybe if you were more specific in your question.
ETA: If you want to save the text box contents to the current row, call ValidateChildren () on the container before the bindingSource.Current property changes.
I have a somewhat similar framework with both grid and textboxes on same form. When user clicks the EDIT button (or Add), I just disable the gridview control itself...
MyDataGrid.Enabled = false;
continue editing..
Then in the SAVE, if all is ok,
MyDataGrid.Enabled = true;

Stuck with datagridview and combo box column

I have a typical requirement.
I have a datagridview with a combobox column(items loaded at design time). When a user selects an item from combobox, remaining rows gets updated in database based on the selectedItem and dgv gets refreshed.
Problem is the combo box will lose its current selection and goes to unselected state.
I want to retain the selected item even after dgv refreshed.
Could anyone help me out
Thanks in advance
Do you mean that you're using an unbound comboboxcolumn? If so, the value can't automatically be persisted when refreshing the datasource. You need to store the selected value before updating and setting it in code after refresh.
If your column is actually databound, the selected value is either not stored in the database or you have some data type problem.
Is the combobox there to let the user select a value for the field or do you use it as a way to execute a command on the record?
Do you have any code you can post?
Datagrid Combo-Box value will retain the string value but will refresh any integer values automatically.
Here is what you need to do :
-When populating Combo-Box value to just convert its value to toString().
-Also if you are setting a default select value also set it with string type.
-Your Combo-box will automatically retain the value selected even after refreshing.
:)
have a combobox in ur datagridview. Assign values to it using a bindingsource.
then write an eventhandler for the datagridviews "EditingControlShowing" event.
in that, remove had handler if any exists for the comboboxes Selectedindexchanged event. then add an event handler for the selectedIndexChanged event say "ComboBoxValueChanged"
in that "ComboBoxValueChanged",
DirectCast the sender to System.Windows.Forms.DataGridViewComboBoxEditingControl and get the selected value of it.
Now use it to compute any value you want.
you may wana refer to this
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxeditingcontrol.aspx

Categories