Getting the selected row from dataGridView1 - c#

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)

Related

Remove column from child gridview

The parent of this grid is Project while the child is BOM. I've manage to display child grid using the following code.
private void gridView_MasterRowEmpty(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowEmptyEventArgs e)
{
Project projects = (Project)gridView.GetRow(e.RowHandle);
e.IsEmpty = projects.BOMs.Count == 0;
}
private void gridView_MasterRowGetRelationCount(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowGetRelationCountEventArgs e)
{
e.RelationCount = 1;
}
private void gridView_MasterRowGetRelationName(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowGetRelationNameEventArgs e)
{
e.RelationName = "BOMs";
}
private void gridView_MasterRowGetChildList(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowGetChildListEventArgs e)
{
Project projects = (Project)gridView.GetRow(e.RowHandle);
e.ChildList = new BindingSource(projects, "BOMs");
}
However, the code is showing me all the columns. I would like to hide some columns from the child which is BOM.
The only way I found was to use something like below
dataGridView1.Columns[index].Visible = false;
But where should I place the above code?
Updated Code
private void gridView_MasterRowExpanded(object sender, DevExpress.XtraGrid.Views.Grid.CustomMasterRowEventArgs e)
{
GridView gridView = sender as GridView;
GridView detailView = (GridView)gridView.GetDetailView(e.RowHandle, e.RelationIndex);
detailView.Columns["Column Name"].Visible = false;
}
To fulfill your need you must handle the Grid_MasterRowExpanded method in your GridControl :
private void Grid_MasterRowExpanded(System.Object sender, DevExpress.XtraGrid.Views.Grid.CustomMasterRowEventArgs e)
{
GridView view = sender;
GridView detail = view.GetDetailView(e.RowHandle, e.RelationIndex);
if (e.RowHandle == 0 | e.RowHandle == 1) {
if (detail.Name == "BOM") {
detail.Columns["Column Name"].Visible = false;
}
}
}
FYI, you can also do this at design-time, which is my favorite way to go. Within the gridView designer, you need to make sure you create a gridView for the master and child. Clicking "Retrieve Details" will do this. It will also blow away any columns you have already created, along with the layout, but the easy way around this is to save the layout as an XML, click "Retrieve Details" and then re-load the XML.
Here is what your designer will look like after you do that:
From here, you can click on each gridView (the master and child separately) and customize each -- change column widths, hide columns, DELETE columns (the data is still there but impossible for the UI to add to the grid), rearrange columns, etc.
And the best part -- no code.

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.

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 to get control inside splitcontainer?

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.

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