Link to home
Create AccountLog in
Avatar of ty-exexch-ko
ty-exexch-koFlag for Albania

asked on

Cells in DataGridView of Visual Basic

How I can display the value of specific cell in DataGridView ?

For example if the cell was row: 4 and column: 6
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

When do you want it ? When clicked on it ?

Check DataGridView.Item(Col, Row).Value


 for the clicked cell ( it will be the current cell)

private void dataGridView1_SelectionChanged(object sender, EventArgs e) {     DataGridView dgv = (DataGridView)sender;      //User selected WHOLE ROW (by clicking in the margin)     if (dgv.SelectedRows.Count> 0)        MessageBox.Show(dgv.SelectedRows[0].Cells[0].Value.ToString());      //User selected a cell (show the first cell in the row)     if (dgv.SelectedCells.Count > 0)         MessageBox.Show(dgv.Rows[dgv.SelectedCells[0].RowIndex].Cells[0].Value.ToString());      //User selected a cell, show that cell     if (dgv.SelectedCells.Count > 0)         MessageBox.Show(dgv.SelectedCells[0].Value.ToString()); }

Open in new window

Avatar of ty-exexch-ko


I will display the cell value without select it

I checked DataGridView.Item(Col, Row).Value and the system give me error
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
I'm using the following code: -

        Dim vr_Test As String()
        Dim i As Long
        For i = 0 To DataGridView1.Rows.Count - 1
            vr_Test(i) = DataGridView1.Rows(i).Cells(1).Value

Open in new window

I get an error
System.NullReferenceException was unhandled
  Message="Object reference not set to an instance of an object."
       at NLP_Project.Form1.Button3_Click(Object sender, EventArgs e)    at System.Windows.Forms.Control.OnClick(EventArgs e)    at System.Windows.Forms.Button.OnClick(EventArgs e)    at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)    at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)    at System.Windows.Forms.Control.WndProc(Message& m)    at System.Windows.Forms.ButtonBase.WndProc(Message& m)    at System.Windows.Forms.Button.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.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)    at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)    at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)    at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)    at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)    at System.Windows.Forms.Application.Run(ApplicationContext context)    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)    at NLP_Project.My.MyApplication.Main(String[] Args)    at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()    at System.Threading.ThreadHelper.ThreadStart_Context(Object state)    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)    at System.Threading.ThreadHelper.ThreadStart()
Try changing to this

Dim vr_Test As New List(Of String)
        Dim i As Long
        For i = 0 To DataGridView1.Rows.Count - 1