This is a powerful control to display data on screen, has many options, try to see a simple example and then mention all that I use when working with a DataGridView.
'Connect to the database and obtaining the records in a DataTable Dim cnn As New SqlConnection cnn.ConnectionString = _ "Data Source = .. \ SQLEXPRESS; Initial Catalog = BasedePrueba; Integrated Security = True " "Some lines of this code had to be divided 'Ekl not enter screen width. cnn.Open () Dim ds As New DataSet Dim da As New SqlDataAdapter Dim sql As String sql = "Select * from SaldosClientes" da.SelectCommand = New SqlCommand (sql, cnn) da.Fill (ds) cnn.Close () 'With this line I get a DataTable '(Dt) with the resulting records Dim dt As DataTable = ds.Tables (0) 'Now I can assign the DataTable (dt) to DataGridView1 DataGridView1.DataSource = dt
With the above code enough to fill our first DataGridView. Now we'll see what else can we do with this control.
'Define which font to use DataGridView1.Font = _ New Drawing.Font ("Tahoma", 8, FontStyle.Regular, GraphicsUnit.Point 'What columns are visible DataGridView1.Columns ("Id"). Visible = False DataGridView1.Columns ("Code"). Visible = True DataGridView1.Columns ("RazonSocial"). Visible = True DataGridView1.Columns ("Date"). Visible = True DataGridView1.Columns ("Balance"). Visible = True 'What will be the order of the columns DataGridView1.Columns ("Id"). DisplayIndex = 0 DataGridView1.Columns ("Code"). DisplayIndex = 1 DataGridView1.Columns ("RazonSocial"). DisplayIndex = 2 DataGridView1.Columns ("Date"). DisplayIndex = 3 DataGridView1.Columns ("Balance"). DisplayIndex = 4 'Width of columns DataGridView1.Columns ("Id"). Width = 100 DataGridView1.Columns ("Code"). Width = 100 DataGridView1.Columns ("RazonSocial"). Width = 100 DataGridView1.Columns ("Date"). Width = 100 DataGridView1.Columns ("Balance"). Width = 100 'The column header DataGridView1.Columns ("Id"). HeaderText = "Id" DataGridView1.Columns ("Code"). HeaderText = "Code" DataGridView1.Columns ("RazonSocial"). HeaderText = "Name" DataGridView1.Columns ("Date"). HeaderText = "Date" DataGridView1.Columns ("Balance"). HeaderText = "Balance" "The alignment of the cells in each column DataGridView1.Columns ("Id"). DefaultCellStyle.Alignment = _ DataGridViewContentAlignment.BottomRight DataGridView1.Columns ("Code"). DefaultCellStyle.Alignment = _ DataGridViewContentAlignment.BottomLeft DataGridView1.Columns ("RazonSocial"). DefaultCellStyle.Alignment = _ DataGridViewContentAlignment.BottomLeft DataGridView1.Columns ("Date"). DefaultCellStyle.Alignment = _ DataGridViewContentAlignment.BottomLeft DataGridView1.Columns ("Balance"). DefaultCellStyle.Alignment = _ DataGridViewContentAlignment.BottomRight 'The alinación header of each column DataGridView1.Columns ("Id"). HeaderCell.Style.Alignment = _ DataGridViewContentAlignment.BottomRight DataGridView1.Columns ("Code"). HeaderCell.Style.Alignment = _ DataGridViewContentAlignment.BottomLeft DataGridView1.Columns ("RazonSocial"). HeaderCell.Style.Alignment = _ DataGridViewContentAlignment.BottomLeft DataGridView1.Columns ("Date"). HeaderCell.Style.Alignment = _ DataGridViewContentAlignment.BottomLeft DataGridView1.Columns ("Balance"). HeaderCell.Style.Alignment = _ DataGridViewContentAlignment.BottomRight 'The format of the numeric columns DataGridView1.Columns ("Balance"). DefaultCellStyle.Format = "# #, # # 0.00"
Well, I spent much time looking for all these settings to leave a DataGridView as I liked. I hope you find it useful and I'll be uploading more soon about this great data control.
[...] 12, 2009 Today I want to show a simple example of how to connect to SQL Server and populate a DataGridView, but without writing a single line of code. This means that we will drag and drop [...]
Thank you,
These examples are very good especially with the simplicity that explain.
Congratulations.
Very good your explanation, but I wonder if you can help me, to see how to do it in reverse, ie, taking the data in a DataGridView (Example 1 row and 4 columns), add a new row in a database the same characteristics (1 row, 4 columns), much appreciate your help. Thank you.
Thanks for the example of filling and configuration of the datagridview, I served well.
Congratulations, plain and simple ... a role model
GRAIAS INFORMATION FOR THIS VERY WELL EXPLAINED MUCH I HAVE BEEN SERVING YOU FOR YOUR HELP.
HEY THANKS FOR THE FOLLOWING INFORMATION SO, EXCELLENTLY EXPLAINED AND FUNCTIONAL TRUTH THAT WE USE THE PROGRAMMERS DATAGRID OFTEN
Hello I have a question that gnaws at me for days,
and find out the index of the row that I have selected in a datagridview.
to form and pass it even taught me the complete details in the form I'm going crazy
I think that will suffice datagridview1.currentrow
Luck
Very good
but I do like to know how to validate data entered in a cell of datagridview column type is date and a maskeditbox.
And to do all kinds of validation
hi all .. q I would like to help me because I'm working with a q q contains an object datagridview DataGridViewComboBoxColumn code i going to see if I help you find an error or do you lack q: Private Sub BtnAddRequisito_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAddRequisito.Click
Dim As String = Negocio.Conexion sConexion
As New DataSet Dim Ds_datos
As String Dim sQuery CboRequisitoColumn.Name = "Subject"
Me.DGVMaterias.Columns.Add (CboRequisitoColumn)
sQuery = "SELECT asg_codigo as code, description FROM asg_nombre as spsatbasignatura WHERE asg_semestre <" & Me.CMBSemestre.Text Ds_datos = Accesar_Datos.GetDataSet (sQuery, sConexion)
my_DGVCboColumn.DataSource = Ds_datos.Tables (0)
my_DGVCboColumn.Name = "Data"
my_DGVCboColumn.DisplayMember = Ds_datos.Tables (0). Columns ("description"). ToString
my_DGVCboColumn.ValueMember = Ds_datos.Tables (0). Columns ("number"). ToString Me.DGVMaterias.Columns.RemoveAt (0)
Me.DGVMaterias.Columns.Insert (0, my_DGVCboColumn)
End Sub'el above creates the grid
'The following is where I want
'Assign or insert the code gives me q
'My_DGVCbocolumn object q is within q DGVMaterias xq I think I should not
'Refer to Sub CboRequisitoColumnPrivate Agregar_Requisitos ()
SQuery Dim As String = ""
Dim As String = Negocio.Conexion sConexion
As New DataSet Dim Ds_Datos
KROW Dim As Integer = 0
Dim aux As String
Try Dim r As Integer = 0 To KROW For Me.DGVMaterias.RowCount - 1
aux = Me.my_DGVCboColumn.Items (KROW). ToString
r = CType (aux, Integer)
sQuery = "spi_spsatbrequisito_asignatura" & Code & "" & r & ""
Ds_Datos = Accesar_Datos.GetDataSet (sQuery, sConexion)
Next KROW
Catch ex As Exception
MsgBox (ex.Message, MsgBoxStyle.Critical, "Error")
TryEnd End Sub 'thanks in advance
Hey. Thanks for this post as useful. I was going crazy trying to guess as to an alienation differently to each column. Anyway, I put the corresponding code in the Load event of form that has the dataGrid and gives me error. I said that I refer to an object that does not exist. Anyone know why this is? Perhaps I should put the code in another event procedure different? Thank you.
Hello .. I hope you read this comment .. I would like to know who owned datagrid catch me no. customer ID to click.
Elemplo ... if I look for people who live in X place and me are 2 in the datagrid, to give you double click the cell of the record number of textbox I filled me with the data. What does is that by giving me 2 records search data last regustro not the first or third .. THANK use a cycle in the event C1_CellDoubleClick (datagrid)
For x As Integer = 0 To (C1.RowCount - 2)
C1.Item Label3.Text = (0, x). Value
Next
Hello! I am new to the DataGridView,
I'm working with Visual Stuio 2008,
My question is:
I have a DataGridView and I am passing the data through a "PictureBox" (product, price, and total catidad)
CLICK to give to the 1st image, the cells filled with a "ImputBox" I asked how much and makes the operation (* price = total amounts)
But ...
To do the same with the next image, or with the same image, DATA I always put in the same ROW!
As I can give to you for every time k of the IMAGES CLCK me handle the row that is ...?
Would greatly appreciate this contribution!
[...] http://www.solovb.net/index.php/2009/01/06/datagridview/ [...]
Thank you very much.
Very good examples.
Greetings friend, thanks for the code
Many thanks for the input, for days and come looking for an example, it suits my code and compile very well ..
I wonder if datagridview control allows you to group several rows with a common field. Can I Do?
Thanks
I HAVE A DOUBT WITH GRIDWIEV, CHANGE THE QUERY AND I DO NOT LOAD THE GRID ON SCREEN THAT CAN BE THIS AFFECT ME WHAT??
GREETINGS AND THANKS
if I specify a bit more the problem and you show me the line in question may be able to help.