hi all,
i m a newer in c#. a already start to using c# today.
in my first project i make program to add,edit and delete data from database.
i m already do all of them. now i want to showing data in every control (textbox,combobox, etc) when i click datagrid.
i want to specify the row that it use as key to binding data, but i dont have any idea for this. does anybody know how to get value from datagrid??

i m using this following code in vb.net. please give a code in c#.net

Dim i As New integer

i = dgGuru.CurrentRowIndex()

cmdGuru = cnn.CreateCommand
cmdGuru.CommandText = "SELECT * FROM PedidikanGuru where NIP ='" + Trim(dgGuru.Item(i, 0)) + "'"

Trim(dgGuru.Item(i, 0)) -> this code will get the value from coloumn 0 and row i (agree with which row i was clicked)

No | Name |
1 |- Jx -|
2 |- Man -|

so ex, if i click row one i will get my textbox1 = 1 and textbox2 = jx.

Please Help me...
Best Regards

I guess that u want print a current DataGridViewCell value to the text box
u should implement the DataGridView_Click(object sender, dataGrideventArgs e) event handler by the appropriate code
public void yourDataGridView_Click(object sender, DataGridViewEventHandler e)
/*Put the value of the current cell that has the focus into oString */
string oString = yourDataGridView[yourDataGridView.CurrentCell.RowIndex,yourDataGridView.CurrentCell.ColumnIndex].ToString();
// then set yourTextBox.text to oString
yourTextBox.Text = oString;

Jugortha has given you the way to get the the current cell into a text box. Have you looked at just binding the Textbox (and other) components to the same BindingSource as the DataGridView... then let the great binding capabiliites handle populating all the controls for you.

hi all,
i was implemented the code correctly but i got this following error :
The type or namespace name 'DataGridViewEventHandler' could not be found (are you missing a using directive or an assembly reference?)

i use vb.net 2003 and sqlserver 2000

i stuck on here, i was written a code but when i click datagrid nothings happened..
this my code that i was tried before :

private void dgUser_Click(object sender, System.EventArgs e)
	int i;
	SqlConnection cnn;
	SqlCommand cmdUser = new SqlCommand();
	SqlDataAdapter daUser = new SqlDataAdapter();
	DataSet dsUser = new DataSet();
	DataTable dtUser = new DataTable();
	i = dgUser.CurrentRowIndex;
	cnn = (System.Data.SqlClient.SqlConnection) Koneksi.GetConnect();
		cmdUser = cnn.CreateCommand();
		cmdUser.CommandText = "SELECT * FROM Users where Id_User ='" + dgUser.GetCellBounds(i,0)   + "'";
		daUser.SelectCommand = cmdUser;
		daUser.Fill(dsUser, "Users");
		dtUser = dsUser.Tables["Users"];
		if (dtUser.Rows.Count > 0)
			txtUserId.Text = System.Convert.ToString(dtUser.Rows[0][0]);
			txtPasw.Text = System.Convert.ToString(dtUser.Rows[0][2]);
			switch (System.Convert.ToString(dtUser.Rows[0][1]))
				case "1":
				cmbStatus.SelectedIndex = 0;
				case "2":
				cmbStatus.SelectedIndex = 1;
				case "3":
				cmbStatus.SelectedIndex = 2;
		catch (Exception ex)
		Interaction.MsgBox("Error: " + ex.Source + ": " + ex.Message, MsgBoxStyle.OKOnly, "Koneksi Error !!");

Please help me.....
best regards..

DataGrid view library is nested in the System.Windows.Forms library so verify if you incuded it or not
I mean In the directive emplacelement have include using System.Windows.Forms ???

include using System.Windows.Forms

yes i m include this file.any suggestion?

private void dgDisp1_CurrentCellChanged(object sender, System.EventArgs e)
			int Sno1;
			dgcell = dgDisp1.CurrentCell;
			cellvalue= dgDisp1[dgcell.RowNumber,0];
				string strQuery="Select * from ExtensionTable where Sno="+Sno1+"";
				OleDbConnection connection = new OleDbConnection(conStr);
				OleDbCommand command = new OleDbCommand(strQuery,connection);	
				OleDbDataReader dataReader;
				dataReader = command.ExecuteReader();

			catch(Exception ex)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.