Get value from Datagrid?

  • Thread starter Thread starter Lasse Edsvik
  • Start date Start date
L

Lasse Edsvik

Hello

I've managed to list data from a database and present with a datagrid like
this:

ProductID Productname
65 A
34 B
11 C

problem now is that when I doubleclick on a row i need to pass the value for
selected ProductID to a function that shows a form like this

ProductID [ 65 ] (textbox)
Productname [ A ] (textbox)

now how do i get value 65 if i doubleclick anywhere on that row so i can
populate a form with the data?

TIA
/Lasse
 
Hello Lasse,

Either use the Items indexer on the DataGrid (quick and dirty way), or get
hold of the DataView managing the bound data source through the
CurrencyManager's List property and retrieve the corresponding data view
row.
The CurrencyManager instance for the bound datasource can be obtained like
this:

CurrencyManager cm = (CurrencyManager)this.BindingContext[myGrid.DataSource,
myGrid.DataMember];
DataView dv = (DataView)dv.List;

The index of the row in question is the value of the CurrencyManager's
Position property.
 
You can trap the double_click event and when the user doubleclicks, you can
reference the currentRowIndex property of the grid. ONce you have that row
you can reference any of the columns in it.

HTH,

Bill
 
This is very easy if you sack off the double click and use a button column.
Failing that you can do the following in the _ItemDataBound method:

private void DataGrid_ItemDataBound_1(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType != ListItemType.Header && e.Item.ItemType !=
ListItemType.Footer)
{
// POP IT INTO THE QUERY STRING THEN RIP IT OUT AT THE DESTINATION
PAGE
e.Item.Attributes["ondblclick"] = "navigate('http://localhost" +
Request.ApplicationPath + "/Page.aspx?ProductID=" +
e.Item.Cells[1].Text.ToString() + "')";
}
}

Hope this helps mate

Ian
 
William,

when i look at the avalible events for datagrid i can not find a dbl click
event? not even any click event

/Lasse


William Ryan eMVP said:
You can trap the double_click event and when the user doubleclicks, you can
reference the currentRowIndex property of the grid. ONce you have that row
you can reference any of the columns in it.

HTH,

Bill
Lasse Edsvik said:
Hello

I've managed to list data from a database and present with a datagrid like
this:

ProductID Productname
65 A
34 B
11 C

problem now is that when I doubleclick on a row i need to pass the value for
selected ProductID to a function that shows a form like this

ProductID [ 65 ] (textbox)
Productname [ A ] (textbox)

now how do i get value 65 if i doubleclick anywhere on that row so i can
populate a form with the data?

TIA
/Lasse
 
Dmitriy,

ive tried to add this:


private void dataGrid1_DoubleClick(object sender, System.EventArgs e)

{


System.Drawing.Point pt = dataGrid1.PointToClient(Cursor.Position);

DataGrid.HitTestInfo hti = dataGrid1.HitTest(pt);


if(hti.Type == DataGrid.HitTestType.Cell)

{

MessageBox.Show("double clicked clicked cell " + hti.Cell.ToString());

}

}



but when i add:



this.dataGrid1.DoubleClick += new
System.Windows.Forms.MouseEventHandler(this.dataGrid1_DoubleClick);




were the rest of them are almost at top of class i get:



C:\Documents and Settings\Administrator\My Documents\Visual Studio
Projects\WindowsApplication3\Form1.cs(98): Method
'WindowsApplication3.Form1.dataGrid1_DoubleClick(object, System.EventArgs)'
does not match delegate 'void System.Windows.Forms.MouseEventHandler(object,
System.Windows.Forms.MouseEventArgs)'


im way off?



/Lasse








Dmitriy Lapshin said:
Hello Lasse,

Either use the Items indexer on the DataGrid (quick and dirty way), or get
hold of the DataView managing the bound data source through the
CurrencyManager's List property and retrieve the corresponding data view
row.
The CurrencyManager instance for the bound datasource can be obtained like
this:

CurrencyManager cm = (CurrencyManager)this.BindingContext[myGrid.DataSource,
myGrid.DataMember];
DataView dv = (DataView)dv.List;

The index of the row in question is the value of the CurrencyManager's
Position property.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://www.x-unity.net/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Lasse Edsvik said:
Hello

I've managed to list data from a database and present with a datagrid like
this:

ProductID Productname
65 A
34 B
11 C

problem now is that when I doubleclick on a row i need to pass the value for
selected ProductID to a function that shows a form like this

ProductID [ 65 ] (textbox)
Productname [ A ] (textbox)

now how do i get value 65 if i doubleclick anywhere on that row so i can
populate a form with the data?

TIA
/Lasse
 
Errrrrm did you seen my post?

Ian

Lasse Edsvik said:
William,

when i look at the avalible events for datagrid i can not find a dbl click
event? not even any click event

/Lasse


William Ryan eMVP said:
You can trap the double_click event and when the user doubleclicks, you can
reference the currentRowIndex property of the grid. ONce you have that row
you can reference any of the columns in it.

HTH,

Bill
Lasse Edsvik said:
Hello

I've managed to list data from a database and present with a datagrid like
this:

ProductID Productname
65 A
34 B
11 C

problem now is that when I doubleclick on a row i need to pass the
value
for
selected ProductID to a function that shows a form like this

ProductID [ 65 ] (textbox)
Productname [ A ] (textbox)

now how do i get value 65 if i doubleclick anywhere on that row so i can
populate a form with the data?

TIA
/Lasse
 
Ian,

dont think it'll work since im building a windows app, not asp.net app

/Lasse


Ian Frawley said:
This is very easy if you sack off the double click and use a button column.
Failing that you can do the following in the _ItemDataBound method:

private void DataGrid_ItemDataBound_1(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType != ListItemType.Header && e.Item.ItemType !=
ListItemType.Footer)
{
// POP IT INTO THE QUERY STRING THEN RIP IT OUT AT THE DESTINATION
PAGE
e.Item.Attributes["ondblclick"] = "navigate('http://localhost" +
Request.ApplicationPath + "/Page.aspx?ProductID=" +
e.Item.Cells[1].Text.ToString() + "')";
}
}

Hope this helps mate

Ian

Lasse Edsvik said:
Hello

I've managed to list data from a database and present with a datagrid like
this:

ProductID Productname
65 A
34 B
11 C

problem now is that when I doubleclick on a row i need to pass the value for
selected ProductID to a function that shows a form like this

ProductID [ 65 ] (textbox)
Productname [ A ] (textbox)

now how do i get value 65 if i doubleclick anywhere on that row so i can
populate a form with the data?

TIA
/Lasse
 
Assuming this is a Winforms or Compact Framwork Grid, it should be there.
Lasse Edsvik said:
William,

when i look at the avalible events for datagrid i can not find a dbl click
event? not even any click event

/Lasse


William Ryan eMVP said:
You can trap the double_click event and when the user doubleclicks, you can
reference the currentRowIndex property of the grid. ONce you have that row
you can reference any of the columns in it.

HTH,

Bill
Lasse Edsvik said:
Hello

I've managed to list data from a database and present with a datagrid like
this:

ProductID Productname
65 A
34 B
11 C

problem now is that when I doubleclick on a row i need to pass the
value
for
selected ProductID to a function that shows a form like this

ProductID [ 65 ] (textbox)
Productname [ A ] (textbox)

now how do i get value 65 if i doubleclick anywhere on that row so i can
populate a form with the data?

TIA
/Lasse
 
The answer is in the error message. Compare the types of the required and
the actual arguments:

(object, System.Windows.Forms.MouseEventArgs)
(object, System.EventArgs)

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://www.x-unity.net/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Lasse Edsvik said:
Dmitriy,

ive tried to add this:


private void dataGrid1_DoubleClick(object sender, System.EventArgs e)

{


System.Drawing.Point pt = dataGrid1.PointToClient(Cursor.Position);

DataGrid.HitTestInfo hti = dataGrid1.HitTest(pt);


if(hti.Type == DataGrid.HitTestType.Cell)

{

MessageBox.Show("double clicked clicked cell " + hti.Cell.ToString());

}

}



but when i add:



this.dataGrid1.DoubleClick += new
System.Windows.Forms.MouseEventHandler(this.dataGrid1_DoubleClick);




were the rest of them are almost at top of class i get:



C:\Documents and Settings\Administrator\My Documents\Visual Studio
Projects\WindowsApplication3\Form1.cs(98): Method
'WindowsApplication3.Form1.dataGrid1_DoubleClick(object, System.EventArgs)'
does not match delegate 'void System.Windows.Forms.MouseEventHandler(object,
System.Windows.Forms.MouseEventArgs)'


im way off?



/Lasse








Dmitriy Lapshin said:
Hello Lasse,

Either use the Items indexer on the DataGrid (quick and dirty way), or get
hold of the DataView managing the bound data source through the
CurrencyManager's List property and retrieve the corresponding data view
row.
The CurrencyManager instance for the bound datasource can be obtained like
this:

CurrencyManager cm = (CurrencyManager)this.BindingContext[myGrid.DataSource,
myGrid.DataMember];
DataView dv = (DataView)dv.List;

The index of the row in question is the value of the CurrencyManager's
Position property.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://www.x-unity.net/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Lasse Edsvik said:
Hello

I've managed to list data from a database and present with a datagrid like
this:

ProductID Productname
65 A
34 B
11 C

problem now is that when I doubleclick on a row i need to pass the
value
for
selected ProductID to a function that shows a form like this

ProductID [ 65 ] (textbox)
Productname [ A ] (textbox)

now how do i get value 65 if i doubleclick anywhere on that row so i can
populate a form with the data?

TIA
/Lasse
 
noo.... there are no such event... im sure

William Ryan eMVP said:
Assuming this is a Winforms or Compact Framwork Grid, it should be there.
Lasse Edsvik said:
William,

when i look at the avalible events for datagrid i can not find a dbl click
event? not even any click event

/Lasse


William Ryan eMVP said:
You can trap the double_click event and when the user doubleclicks,
you
can
reference the currentRowIndex property of the grid. ONce you have
that
row
you can reference any of the columns in it.

HTH,

Bill
Hello

I've managed to list data from a database and present with a
datagrid
like
this:

ProductID Productname
65 A
34 B
11 C

problem now is that when I doubleclick on a row i need to pass the value
for
selected ProductID to a function that shows a form like this

ProductID [ 65 ] (textbox)
Productname [ A ] (textbox)

now how do i get value 65 if i doubleclick anywhere on that row so i can
populate a form with the data?

TIA
/Lasse
 
Ah right my mistake sorry.

Ian

Lasse Edsvik said:
Ian,

dont think it'll work since im building a windows app, not asp.net app

/Lasse


Ian Frawley said:
This is very easy if you sack off the double click and use a button column.
Failing that you can do the following in the _ItemDataBound method:

private void DataGrid_ItemDataBound_1(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType != ListItemType.Header && e.Item.ItemType !=
ListItemType.Footer)
{
// POP IT INTO THE QUERY STRING THEN RIP IT OUT AT THE DESTINATION
PAGE
e.Item.Attributes["ondblclick"] = "navigate('http://localhost" +
Request.ApplicationPath + "/Page.aspx?ProductID=" +
e.Item.Cells[1].Text.ToString() + "')";
}
}

Hope this helps mate

Ian

Lasse Edsvik said:
Hello

I've managed to list data from a database and present with a datagrid like
this:

ProductID Productname
65 A
34 B
11 C

problem now is that when I doubleclick on a row i need to pass the
value
for
selected ProductID to a function that shows a form like this

ProductID [ 65 ] (textbox)
Productname [ A ] (textbox)

now how do i get value 65 if i doubleclick anywhere on that row so i can
populate a form with the data?

TIA
/Lasse
 
Dmitriy,

this.dataGrid1.DoubleClick += new
System.Windows.Forms.MouseEventHandler(this.dataGrid1_DoubleClick);

private void dataGrid1_DoubleClick(object sender,
System.Windows.Forms.MouseEventArgs e)

is wrong too?



i get:

C:\Documents and Settings\Administrator\My Documents\Visual Studio
Projects\WindowsApplication3\Form1.cs(98): Cannot implicitly convert type
'System.Windows.Forms.MouseEventHandler' to 'System.EventHandler'








Dmitriy Lapshin said:
The answer is in the error message. Compare the types of the required and
the actual arguments:

(object, System.Windows.Forms.MouseEventArgs)
(object, System.EventArgs)

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://www.x-unity.net/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Lasse Edsvik said:
Dmitriy,

ive tried to add this:


private void dataGrid1_DoubleClick(object sender, System.EventArgs e)

{


System.Drawing.Point pt = dataGrid1.PointToClient(Cursor.Position);

DataGrid.HitTestInfo hti = dataGrid1.HitTest(pt);


if(hti.Type == DataGrid.HitTestType.Cell)

{

MessageBox.Show("double clicked clicked cell " + hti.Cell.ToString());

}

}



but when i add:



this.dataGrid1.DoubleClick += new
System.Windows.Forms.MouseEventHandler(this.dataGrid1_DoubleClick);




were the rest of them are almost at top of class i get:



C:\Documents and Settings\Administrator\My Documents\Visual Studio
Projects\WindowsApplication3\Form1.cs(98): Method
'WindowsApplication3.Form1.dataGrid1_DoubleClick(object, System.EventArgs)'
does not match delegate 'void System.Windows.Forms.MouseEventHandler(object,
System.Windows.Forms.MouseEventArgs)'


im way off?



/Lasse








in message news:[email protected]...
Hello Lasse,

Either use the Items indexer on the DataGrid (quick and dirty way), or get
hold of the DataView managing the bound data source through the
CurrencyManager's List property and retrieve the corresponding data view
row.
The CurrencyManager instance for the bound datasource can be obtained like
this:

CurrencyManager cm = (CurrencyManager)this.BindingContext[myGrid.DataSource,
myGrid.DataMember];
DataView dv = (DataView)dv.List;

The index of the row in question is the value of the CurrencyManager's
Position property.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://www.x-unity.net/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Hello

I've managed to list data from a database and present with a
datagrid
like
this:

ProductID Productname
65 A
34 B
11 C

problem now is that when I doubleclick on a row i need to pass the value
for
selected ProductID to a function that shows a form like this

ProductID [ 65 ] (textbox)
Productname [ A ] (textbox)

now how do i get value 65 if i doubleclick anywhere on that row so i can
populate a form with the data?

TIA
/Lasse
 
Aha, it seems I now see the problem. You should declare
dataGrid1_DoubleClick as:

private void dataGrid1_DoubleClick(object sender,
System.EventArgs e)

and attach the handler like this:

this.dataGrid1.DoubleClick += new
System.EventHandler(this.dataGrid1_DoubleClick);

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://www.x-unity.net/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Lasse Edsvik said:
Dmitriy,

this.dataGrid1.DoubleClick += new
System.Windows.Forms.MouseEventHandler(this.dataGrid1_DoubleClick);

private void dataGrid1_DoubleClick(object sender,
System.Windows.Forms.MouseEventArgs e)

is wrong too?



i get:

C:\Documents and Settings\Administrator\My Documents\Visual Studio
Projects\WindowsApplication3\Form1.cs(98): Cannot implicitly convert type
'System.Windows.Forms.MouseEventHandler' to 'System.EventHandler'








Dmitriy Lapshin said:
The answer is in the error message. Compare the types of the required and
the actual arguments:

(object, System.Windows.Forms.MouseEventArgs)
(object, System.EventArgs)

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://www.x-unity.net/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Lasse Edsvik said:
Dmitriy,

ive tried to add this:


private void dataGrid1_DoubleClick(object sender, System.EventArgs e)

{


System.Drawing.Point pt = dataGrid1.PointToClient(Cursor.Position);

DataGrid.HitTestInfo hti = dataGrid1.HitTest(pt);


if(hti.Type == DataGrid.HitTestType.Cell)

{

MessageBox.Show("double clicked clicked cell " + hti.Cell.ToString());

}

}



but when i add:



this.dataGrid1.DoubleClick += new
System.Windows.Forms.MouseEventHandler(this.dataGrid1_DoubleClick);




were the rest of them are almost at top of class i get:



C:\Documents and Settings\Administrator\My Documents\Visual Studio
Projects\WindowsApplication3\Form1.cs(98): Method
'WindowsApplication3.Form1.dataGrid1_DoubleClick(object, System.EventArgs)'
does not match delegate 'void System.Windows.Forms.MouseEventHandler(object,
System.Windows.Forms.MouseEventArgs)'


im way off?



/Lasse








in message Hello Lasse,

Either use the Items indexer on the DataGrid (quick and dirty way),
or
get
hold of the DataView managing the bound data source through the
CurrencyManager's List property and retrieve the corresponding data view
row.
The CurrencyManager instance for the bound datasource can be
obtained
like
this:

CurrencyManager cm =
(CurrencyManager)this.BindingContext[myGrid.DataSource,
myGrid.DataMember];
DataView dv = (DataView)dv.List;

The index of the row in question is the value of the CurrencyManager's
Position property.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://www.x-unity.net/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Hello

I've managed to list data from a database and present with a datagrid
like
this:

ProductID Productname
65 A
34 B
11 C

problem now is that when I doubleclick on a row i need to pass the value
for
selected ProductID to a function that shows a form like this

ProductID [ 65 ] (textbox)
Productname [ A ] (textbox)

now how do i get value 65 if i doubleclick anywhere on that row so
i
can
populate a form with the data?

TIA
/Lasse
 
udaman Dmitriy :)

thanks for helping, i'll post lots new questions in a while lol

/Lasse


Dmitriy Lapshin said:
Aha, it seems I now see the problem. You should declare
dataGrid1_DoubleClick as:

private void dataGrid1_DoubleClick(object sender,
System.EventArgs e)

and attach the handler like this:

this.dataGrid1.DoubleClick += new
System.EventHandler(this.dataGrid1_DoubleClick);

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://www.x-unity.net/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Lasse Edsvik said:
Dmitriy,

this.dataGrid1.DoubleClick += new
System.Windows.Forms.MouseEventHandler(this.dataGrid1_DoubleClick);

private void dataGrid1_DoubleClick(object sender,
System.Windows.Forms.MouseEventArgs e)

is wrong too?



i get:

C:\Documents and Settings\Administrator\My Documents\Visual Studio
Projects\WindowsApplication3\Form1.cs(98): Cannot implicitly convert type
'System.Windows.Forms.MouseEventHandler' to 'System.EventHandler'








in message news:[email protected]...
The answer is in the error message. Compare the types of the required and
the actual arguments:

(object, System.Windows.Forms.MouseEventArgs)
(object, System.EventArgs)

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://www.x-unity.net/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Dmitriy,

ive tried to add this:


private void dataGrid1_DoubleClick(object sender, System.EventArgs e)

{


System.Drawing.Point pt = dataGrid1.PointToClient(Cursor.Position);

DataGrid.HitTestInfo hti = dataGrid1.HitTest(pt);


if(hti.Type == DataGrid.HitTestType.Cell)

{

MessageBox.Show("double clicked clicked cell " + hti.Cell.ToString());

}

}



but when i add:



this.dataGrid1.DoubleClick += new
System.Windows.Forms.MouseEventHandler(this.dataGrid1_DoubleClick);




were the rest of them are almost at top of class i get:



C:\Documents and Settings\Administrator\My Documents\Visual Studio
Projects\WindowsApplication3\Form1.cs(98): Method
'WindowsApplication3.Form1.dataGrid1_DoubleClick(object,
System.EventArgs)'
does not match delegate 'void
System.Windows.Forms.MouseEventHandler(object,
System.Windows.Forms.MouseEventArgs)'


im way off?



/Lasse








in message Hello Lasse,

Either use the Items indexer on the DataGrid (quick and dirty
way),
or
get
hold of the DataView managing the bound data source through the
CurrencyManager's List property and retrieve the corresponding
data
view
row.
The CurrencyManager instance for the bound datasource can be obtained
like
this:

CurrencyManager cm =
(CurrencyManager)this.BindingContext[myGrid.DataSource,
myGrid.DataMember];
DataView dv = (DataView)dv.List;

The index of the row in question is the value of the CurrencyManager's
Position property.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://www.x-unity.net/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Hello

I've managed to list data from a database and present with a datagrid
like
this:

ProductID Productname
65 A
34 B
11 C

problem now is that when I doubleclick on a row i need to pass the
value
for
selected ProductID to a function that shows a form like this

ProductID [ 65 ] (textbox)
Productname [ A ] (textbox)

now how do i get value 65 if i doubleclick anywhere on that row
so
 
In the ondouble click event procedure, the second parameter is 'e'
object. Use this object

Label1.Text=e.item.cells(0).Controls(0)

with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 
Back
Top