how to get control inside splitcontainer? - c#

My code is
((DataGridView)(Application.OpenForms["frmMasterInterface"]
.Controls["splitContainer1"]
.Controls["splitContainer1.Panel1"]
.Controls["pnlLeft"]
.Controls["grbfittingTypes"]
.Controls["dgvitems"])).Rows[RowIndex].Cells["sizeRange"].Value
I want to get value of row of datagridview from another form.
Please help.
Thanks & Regards,
-Nitin

There are probably hundreds of ways to do this, I've just tried the following, first getting a reference to the split container and then accessing Panel1 directly, and it works:
SplitContainer sc = (SplitContainer)Application.OpenForms[0].Controls["splitContainer1"];
DataGridView dg = (DataGridView)sc.Panel1.Controls["dataGridView1"];
That said, if I was you I'd look at instead creating a property on your frmMasterInterface to access the DataGridView directly, and then passing a reference to frmMasterInterface to the second form.

i Hope that you wanna get the data by click the cell content.from a differnt.It works for me as u defined ,hope it helps to you.
DataGridView dggg; //Globally Declared
private void dd()
{
var form = (frmMasterInterface)Application.OpenForms["frmMasterInterface"];//Form Name
if (form == null) return;
foreach (DataGridView dgv in form.Controls.OfType<DataGridView>())
if (dgv.Name == "dataGridView1") //name of data grid view
{
dggg = dgv;
dgv.CellMouseClick += Datagirdmouseclick;
}
}
private void Datagirdmouseclick(object sender, DataGridViewCellMouseEventArgs e)
{
Trace.WriteLine(dggg.Rows[e.RowIndex].Cells["sizeRange"].Value);
}
Hope it helps.

Related

how to pass datagridview value to another form when the datagridviewbutton cell is clicked

I've been working on my datagridview properties and wondering how can I pass the value of my selected data row to another form.
private void dgvRptView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
var senderGrid = (DataGridView)sender;
if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn &&
e.RowIndex >= 0)
{
Form Update = new frmUpdateSvcRep();
Update.Show();
}
}
apparently, I was only able to add button inside the datagridview and added an event when I clicked the button, it'll show a form. However. I've been trying to pass the values I selected to textbox in another from but to no avail. Can someone please help me figure out how to pass the value where I click my button? here's my image caption.
here's my another form looks like when I click the edit button inside the Datagridview.
I'm really out of my depth for now.. I'm opt on creating constructors but I don't know how to implement it in this Scenario. Thanks in Advance
There are several ways to accomplish passing data between Forms. One way, as you mentioned, is to pass via the constructor when you instantiate the Form:
private void dgvRptView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (dgvRptView.Columns[e.ColumnIndex] is DataGridViewButtonColumn &&
e.RowIndex >= 0)
if (dgvRptView.CurrentRow != null)
{
var row = dgvRptView.CurrentRow.Cells;
DateTime age = Convert.ToDateTime(row["MyColumn"].Value);
string name = Convert.ToString(row["MyName"].Value);
Form Update = new frmUpdateSvcRep(age, name);
Update.Show();
}
}
Update the constructor of your other Form to accept those parameters:
public class Update : Form
{
public Update(DateTime age, string name)
{
// do whatever you want with the parameters
}
...
}
It might be tempting to just pass the entire dgvRptView.CurrentRow object, but I'd advise against that. Your other Form then has to know about the columns in the DataGridView so it can access the values, which is not something it should be concerned about and could lead to runtime bugs when the column names change.

Getting the selected row from dataGridView1

I have a dataGridView1 on my Wondows Form Application. I have populated the dataGridView1 with a list of strings. This all works great. But...
I need to be able to grab the row the user is selected on and I can't seem to do it. My code just gives me an error, written below.
I have copied GridViewRow form the msdn site.
private void editButton_Click(object sender, EventArgs e) {
GridViewRow row = dataGridView1.SelectedRow;
//This is what I have been using before with a list box.
if (itemList.SelectedIndex >= 0) {
int newInt = itemList.SelectedIndex;
Form form = new NewItemW(selectedFolder, this, items[newInt], WindowEnum.ItemEdit);
form.Show();
}
}
I have tried this but I am getting the error: "The type or namespace 'GridViewRow' could not be found."
My basic question is how do I get this to work?
Assuming you are using a Winforms DataGridView you should do it like this:
private void editButton_Click(object sender, EventArgs e)
{
if (DataGridView1.SelectedRows.Count > 0)
{
DataGridViewRow row = dataGridView1.SelectedRows[0];
if (itemList.SelectedIndex >= 0)
{
int newInt = itemList.SelectedIndex;
Form form = new NewItemW(
selectedFolder, this, items[newInt], WindowEnum.ItemEdit);
form.Show();
}
}
}
By copying from that link you have mixed the WPF Controls and their code with Winforms. Often folks mix up just the names DataGridView vs GridViewRow, but you have also copied the wong Property SelectedRow. Instead use the first element of the SelectedRows collection. Also always check or else the array acess will crash..
you are using the web control System.Web.UI.WebControls.WebControl.GridViewRow, it must be hosted in a web page.
For windows form use the System.Windows.Forms.DataGridViewRow control.
http://msdn.microsoft.com/en-us/library/vstudio/system.windows.forms.datagridviewrow(v=vs.110)

How to activate combobox on first click (Datagridview)

In winforms, you need to click the combobox twice to properly activate it - the first time to focus it, the second time to actually get the dropdown list.
How do I change this behavior so that it activates on the very first click?
This is for DATAGRIDVIEW combobox.
I realize this is an old question, but I figured I would give my solution to anyone out there that may need to be able to do this.
While I couldn't find any answers to do exactly this... I did find an answer to a different question that helped me.
This is my solution:
private void datagridview_CellEnter(object sender, DataGridViewCellEventArgs e)
{
bool validClick = (e.RowIndex != -1 && e.ColumnIndex != -1); //Make sure the clicked row/column is valid.
var datagridview = sender as DataGridView;
// Check to make sure the cell clicked is the cell containing the combobox
if(datagridview.Columns[e.ColumnIndex] is DataGridViewComboBoxColumn && validClick)
{
datagridview.BeginEdit(true);
((ComboBox)datagridview.EditingControl).DroppedDown = true;
}
}
private void datagridview_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
datagridview.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
The above code must be tied into the CellEnter event of the datagridview.
I hope this helps!
edit: Added a column index check to prevent crashing when the entire row is selected.
Thanks, Up All Night for the above edit
edit2: Code is now to be tied to the CellEnter rather than the CellClick event.
Thanks, HaraldDutch for the above edit
edit3: Any changes will committed immediately, this will save you from clicking in another cell in order to update the current combobox cell.
Set the following on your DataGridView:
EditMode = EditOnEnter
This is probably the easiest solution and has been the workaround for many users here on SO when this question gets asked.
EDIT :
Per here do the following:
Set the Editmode:
EditMode = EditOnKeystrokeOrF2
Modify the EditingControlShowing event on the datagridview:
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox ctl = e.Control as ComboBox;
ctl.Enter -= new EventHandler(ctl_Enter);
ctl.Enter += new EventHandler(ctl_Enter);
}
void ctl_Enter(object sender, EventArgs e)
{
(sender as ComboBox).DroppedDown = true;
}
This will get you your desired results. Let me know if that doesn't do it.
I changed only the EditMode property of the datagridview to EditOnEnter and it's working perfectly.
EditMode = EditOnEnter
If you set the entire grid to EditOnEnter, you can get some pretty funky activity when you are on a text column. Here's my solution, which should be self explanatory. If you did not know the column names, you could just check the cell type on mousemove.
Private Sub GridView_CellMouseMove(sender As Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles GridView.CellMouseMove
Select Case GridView.Columns(e.ColumnIndex).Name
Case "Ad_Edit", "Size_Caption", "Demo_Code"
GridView.EditMode = DataGridViewEditMode.EditOnEnter
Case Else
GridView.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2
End Select
End Sub
Set the DropDownStyle property of your combo box to DropDownList...
Perhaps old.. But make sure to set ReadOnly property to false, else the cell wont enter editmode and therefore the EditingControl returns null and casting DroppedDown = true will cast a NullReferencException.

datagridview using a datatable with a column href link

I'm trying to figure out how to get a bounded datagridview column in my c# winform project to show up like an href link. The thing is that the link click works but any average user wouldn't realize that they can click the field since it's displayed as a string. I need the field to show up as blue, with underlines, the mouse pointer turns into a hand ...etc.
I was able to accomplish this previously when I was using Datasets with my Datagrid. I went to the designer and selected "Add Column" and added it as a 'DataGridViewLinkColumn". I've recently changed the project to use datatables and I realized that the fields no longer show up as clickable (if I click it does work though).
Any ideal how to accomplish this with relative ease? I've searched and I'm somewhat surprised that I cannot seem to find a simple solution.
Change the type of the cells that are links to be a DataGridViewLinkCell and then handle the click on the cell, like this:
void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
foreach (DataGridViewRow r in dataGridView1.Rows)
{
if (System.Uri.IsWellFormedUriString(r.Cells["Links"].Value.ToString(), UriKind.Absolute))
{
r.Cells["Links"] = new DataGridViewLinkCell();
DataGridViewLinkCell c = r.Cells["Links"] as DataGridViewLinkCell;
}
}
}
// And handle the click too
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex] is DataGridViewLinkCell)
{
System.Diagnostics.Process.Start( dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value as string);
}
}
This might help:
DataGridViewLinkColumn col1 = new DataGridViewLinkColumn();
dataGridView1.Columns.Add(col1);
dataGridView1.Columns[0].Name = "Links";
DataGridViewRow dgvr = new DataGridViewRow();
dgvr.CreateCells(dataGridView1);
DataGridViewCell linkCell = new DataGridViewLinkCell();
linkCell.Value = #"http:\\www.google.com";
dgvr.Cells[0] = linkCell;
dataGridView1.Rows.Add(dgvr);
it creates a col and then a cell of type link.
you can use foreach loops to do this more orderly and faster for more items.
Good Luck!
Take a look at the DataGridViewLinkColumn.LinkBehavior Property. It can be set to AlwaysUnderline.
As for color, simply use the *LinkColor properties on the DataGridViewLinkColumn.
Cheers
You could just colour that column in the datagridview. You could do this in a DataBindingComplete event like so:
private void dataGridView1_DataBindingComplete(object sender,
DataGridViewBindingCompleteEventArgs e)
{
if(this.mydatagridview.Columns["YourLinkColumnName"] != null)
{
this.mydatagridview.Columns["YourLinkColumnName"].DefaultCellStyle.Font = ...
this.mydatagridview.Columns["YourLinkColumnName"].DefaultCellStyle.ForeColor = ...
}
}
You can set the font to be however you like it (ie. underlined, colored, etc.).
Alternatively, you can change the default cell style in the designer if you have the columns premade (not autogeneratedcolumns).

How could i load a form corresponding to the cell clicked in datagrid

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.

Categories