how can we pick value of selected cell from datagrid view

how can we pick value of selected cell from datagrid view

Page 1 of 1

5 Replies - 27909 Views - Last Post: 27 November 2009 - 12:01 PM Rate Topic: -----

#1 shahid manzoor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-January 09

how can we pick value of selected cell from datagrid view

Posted 20 January 2009 - 07:18 PM

how can we pick value of selected cell from datagrid view

private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
//labll should get value from selected cell on
label1.Text = dataGridView1.SelectedCells[e.selectedCells].Value.ToString();
}

This post has been edited by shahid manzoor: 21 January 2009 - 12:38 PM

Is This A Good Question/Topic? 0
  • +

Replies To: how can we pick value of selected cell from datagrid view

#2 shahid manzoor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-January 09

Re: how can we pick value of selected cell from datagrid view

Posted 20 January 2009 - 07:26 PM

View Postshahid manzoor, on 20 Jan, 2009 - 06:18 PM, said:

how can we pick value of selected cell from datagrid view

private void button1_Click(object sender, EventArgs e)
{
//labll should get value from selected cell
label1.Text = dataGridView1.SelectedCells;
}

Was This Post Helpful? 0
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: how can we pick value of selected cell from datagrid view

Posted 20 January 2009 - 08:08 PM

this will get the value of the first selected cell.
dataGridView1.SelectedCells[0].Value


Was This Post Helpful? 0
  • +
  • -

#4 scalt  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 342
  • Joined: 22-November 07

Re: how can we pick value of selected cell from datagrid view

Posted 20 January 2009 - 08:09 PM

You're on the right track. SelectedCells is a collection of cells that have been selected by the user (even if only one has been selected, it is still a collection). To get to the selected cell, assume it is the first cell in the collection (it will be). You then have to get its value as a string.

In short, your answer is:
label1.Text = dataGridView1.SelectedCells[0].Value.ToString();


Was This Post Helpful? 0
  • +
  • -

#5 getajob1  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 03-February 09

Re: how can we pick value of selected cell from datagrid view

Posted 20 February 2009 - 09:55 AM


Let's take this a step further. Let's say you want to pass multiple values of the row cells selected and
process or send these values to the next screen. First set the SelectionMode property of the DataGridView = FullRowSelect.
This will highlight (select) the entire row, then since you know the order of the columns on the grid, use the following code
which is initiated upon the user clicks (selects) the grid row.

 private void gridColors_CellContentClick(object sender, DataGridViewCellEventArgs e)
		{		   
			string CustId = gridColors.SelectedCells[0].Value.ToString();
			string CompName = gridColors.SelectedCells[1].Value.ToString();
			string ContName = gridColors.SelectedCells[2].Value.ToString();
			string ContTitle = gridColors.SelectedCells[3].Value.ToString();
			string Address = gridColors.SelectedCells[4].Value.ToString();
			string City = gridColors.SelectedCells[5].Value.ToString();
			string Region = gridColors.SelectedCells[6].Value.ToString();
			string ZipCode = gridColors.SelectedCells[7].Value.ToString();
			string Country = gridColors.SelectedCells[8].Value.ToString();
			string PhoneNbr = gridColors.SelectedCells[9].Value.ToString();
			string FaxNbr = gridColors.SelectedCells[10].Value.ToString();

			frmChild newfrmChild = new frmChild(CustId, CompName, ContName, ContTitle, Address, City, Region, ZipCode, Country, PhoneNbr, FaxNbr);
			newfrmChild.ShowDialog();
			newfrmChild.Dispose();
 

This post has been edited by getajob1: 20 February 2009 - 10:12 AM

Was This Post Helpful? 1

#6 zishanhaider  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 27-November 09

Re: how can we pick value of selected cell from datagrid view

Posted 27 November 2009 - 12:01 PM

I am having a problem with the same.. Following is my code..

[code] datagrid1.SelectedCell[0].Value.ToString() </code>

Whenever I run the program it throws me the following exception:

System.NullReferenceException: Object reference not set to an instance of an object.
at WindowsFormsApplication1.frmPOS.Control_KeyPressData(Object sender, KeyPressEventArgs e) in C:\Users\Zishan Haider\Documents\Visual Studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\frmPOS.cs:line 106
at System.Windows.Forms.Control.onkeypress(KeyPressEventArgs e)
at System.Windows.Forms.Control.ProcessKeyEventArgs(Message& m)
at System.Windows.Forms.DataGridViewTextBoxEditingControl.ProcessKeyEventArgs(Message& m)
at System.Windows.Forms.Control.ProcessKeyMessage(Message& m)
at System.Windows.Forms.Control.WmKeyChar(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.TextBoxBase.WndProc(Message& m)
at System.Windows.Forms.TextBox.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativewindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativewindow.WndProc(Message& m)
at System.Windows.Forms.Nativewindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


It seems to me that Value property is not referenced. I am using Visual Studio 2010 beta II. Can anyone help me with that??
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1