Click here to Skip to main content
15,889,200 members
Everything / DataGridView

DataGridView

DataGridView

Great Reads

by Shweta Lodha
Reducing flicker and blinking in DataGridView.
by Dukhabandhu Sahoo
This article explains how to display a loading indicator (a GIF image) in Windows Forms application when some long running task is performed in the background.
by Super Lloyd
A fully featured yet short (only 436 lines) replacement for datatables
by Ger Hayden
C++ DateTimePicker in DataGridView

Latest Articles

by kevinuni
This is an alternative for "Master Detail Datagridview"
by WyoMetz
Simple and easy paging of a WPF DataGrid with DataTable and LINQ queries
by Wade Harvey
This article shows how to use Winforms, WPF and C# to create a File Explorer Clone with Tabs and an extremely fast parallel file search utility.
by Dukhabandhu Sahoo
This article explains how to display a loading indicator (a GIF image) in Windows Forms application when some long running task is performed in the background.

All Articles

Sort by Title

DataGridView 

19 Feb 2020 by bgcwaterman
I am getting the error when I select(click) on the row I want to get the customer number from. The Datagridview has over 200 rows and 9 columns. I want the contents of the second column(column 1) that shows on the Datagridview. The Datagridview...
19 Feb 2020 by phil.o
It may be because you are using the SelectedRows collection; is there any chance that clicking on a cell does not automatically select the corresponding row? Why not using the event arguments for that? AssgnCusNo =...
5 Feb 2011 by Eddie Niebruegge
When I Build my project, I get the following error on my e.cancel statement:'cancel' is not a member of 'System.Windows.Forms.DataGridViewCellEventArgs'Here's the code in my event:Private Sub dgvPhysCME_CellValidating(ByVal sender As System.Object, ByVal e As...
5 Feb 2011 by OriginalGriff
Try e.Cancel instead: C# and VB are case sensitive.Additionally, you are using the wrong eventargs: DataGridViewCellValidatingEventArgs is what you should have. See MSDN[^] for details.
5 Feb 2011 by Estys
You handle the wrong event.It must be :private void dgvPhysCME_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) Handles dgvPhysCME.CellValidatingThe DataGridViewCellValidatingEventArgs has a Cancel property.Cheers
19 Dec 2013 by agent_kruger
i have developed an application and it is running fine. But when i uploded the software to one of my client machine it gives the following error.system.security.permissions.hostprotectionattribute from assembly mscorlib.I Am using c# windows application and database is access.Note: On...
19 Dec 2013 by Dave Kreskowiak
Are they running the .EXE from a network share?
21 May 2014 by agent_kruger
Solved it :- as anti-virus software and firewall were not granting me permission.
13 Nov 2018 by ilostmyid2
In a WinForm application, I've a DataGridView control. I don't add any column to it. I just set its DataSource to a list. The list is of type Field which is a class I wrote. It contains of three members, Name of type string, Type of type string and Len of type int. This causes no problem and the...
31 May 2010 by Sandeep Mewara
Why to repost it? Just update the existing question. Please follow at one place: http://www.codeproject.com/Questions/84690/GridView-OnRowUpdating-Event.aspx[^]
18 May 2010 by Sandeep Mewara
Please don't post multiple times. Stick to one. http://www.codeproject.com/Questions/82065/how-make-bill-in-datagridview.aspx[^]
21 May 2017 by nyt1972
Dear attached is my project I wanted to store rows from datagridview to mysql table but I am facing the error in the image kindly help me. http://ansicollege.net/vbcode/error.png[^] This the error I get. Following is the code line I get error in dgvcc =...
20 May 2017 by RickZeeland
As the error message says 'product_id' can not be null, depending on how your table is defined you will have to either supply a value for it, or DEFAULT. See: MySQL :: MySQL 5.7 Reference Manual :: 13.2.5 INSERT Syntax[^] You probably need another cast to the type of 'product_id' in: dgvcc.Value
6 Apr 2011 by garfield185
Hi.I am trying to load some information from a query into a datagridView. This query return an integer and some decimals. After loading the information into the DataGridView , I need to take that first number and format it ( it is a date, so I change it from (int) yyyymmdd format to...
6 Apr 2011 by garfield185
I solved it changing a little the sql statement:MYDATE (INT TYPE ON DATABASE)Instead of doing :SELECT MYDATE FROM TABLEwhich made the field on the DataGridView be an Int32, I changed to:SELECT CAST(MYDATE AS VARCHAR(8)) MYDATE FROM TABLEThis way, the DataGridView...
29 Jun 2010 by AugustinJ
Hi.I want to make a custom DataGridViewCell like a DataGridViewNumericUpDownCellI have the following classes:public class DataGridViewNumericUpDownColumn : DataGridViewTextBoxColumn{ //...}public class DataGridViewNumericUpDownCell : DataGridViewTextBoxCell{ ...
29 Jun 2010 by AugustinJ
FOUND IT!!public override void PositionEditingControl(bool setLocation, bool setSize, System.Drawing.Rectangle cellBounds, System.Drawing.Rectangle cellClip, DataGridViewCellStyle cellStyle, bool singleVerticalBorderAdded, bool singleHorizontalBorderAdded, bool isFirstDisplayedColumn, bool...
9 Mar 2011 by RadethDart
Alright, I am having the hardest time figuring this out. I have done research on fixing this for a couple days and I am stuck. It is time to ask for help.In my project I am trying to delete a row out of the database and I read how to do that from many different sources telling me to use Ole...
10 Mar 2011 by Dave Kreskowiak
I suggest reading the documentation on AcceptChanges and Update. Update writes the dirty records in the DataSet/DataTable back to the database. AcceptChanges tells the DataSet/DataTable that the changes that were made are now the current state of the records. The two operations are completely...
27 Feb 2014 by aolin.wang
Hi,all my friends!When i write program,i run into two problems!1.After click "Update" button,i click "Show" button,the datagridview will not display data.2.The second click "Update" button,message box show:"Violation of Concurrency: UpdateCommand affects the expected 1 records in...
7 May 2014 by prasunjeet
i have an query please ans me as fast as posible i need urgentlyi have a grid view which contains some controls like radio button and text box. i want to save these details to their corresponding id of student in table attendancei want to store the attendance detail of the studenthtml code...
7 May 2014 by Micky Solve
Hi,Please use TextChanged event of textbox, and in the code behind check whether the radio button is checked or not..Like this...protected void YourTextboxId_TextChanged(object sender, EventArgs e) { string value; TextBox txtcityTitle...
7 May 2014 by Micky Solve
Please check this..http://www.aspforums.net/Threads/400418/Insert-multiple-rows-from-GridView-to-Database-in-ASPNet/
25 Sep 2013 by Ioan-Alexandru
I have thiw grid view in which i load from the database 3 columns. Looks like this ID| Server| Status 1| Tm2323| checked 2| Tm2356| unchecked 3| Xg2355| checked 4| Hgd345| checkedSo i want to do this uncheck or check a server in the third column of the...
25 Sep 2013 by mohamed ibrahim
use the save method when the (cell end edit) has been fired
25 Sep 2013 by Ioan-Alexandru
string SQLString; SQLString = @"SELECT tblServers.ID, tblServers.ServerName, tblServers.Status FROM (tblServers)"; OleDbCommand SQLQuery = new OleDbCommand(); DataTable data = null; ...
4 Apr 2013 by shacha
I have a datagridview and i have added a textbox column inside it and i want to get the value entered in the textbox how can i do it?
4 Apr 2013 by Sergey Alexandrovich Kryukov
Let's suppose you know the instance of the cell, have its reference; and the cell should be of the runtime type System.Windows.Forms.DataGridViewTextBoxCell:http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewtextboxcell.aspx[^].For your purpose, even if you only have...
31 May 2023 by Ankit Goel
I want to somehow pass the reference (from form1.cs class) and access the reference of datagridview1 which is used only in Form1.cs in my declaration of customdatagridview . The reason is that in every form of my windows application , there will...
31 May 2023 by Alan N
The Control class has a useful FindForm() method to do just what you want. It walks up the chain of Control.Parent properties until it comes to a Control which IS A Form. There is also a Control.TopLevelControl property which in most cases will...
31 May 2023 by Maciej Los
If you want to transfer data between forms, then... Stop thinking about a datagridview object! And start thinking about the data. I'd stringly recommend to read these articles: Transferring information between two forms, Part 1: Parent to...
13 Jan 2014 by JOHNNYDEMICHAEL
csvData is a DataTable bound to a DataGridView. Compiles & runs no errors . . but after refresh, no new row appears in grid . . . ?? Is there something missing? private void addNewRowButton_Click(object sender, EventArgs e) { csvData.NewRow(); ...
13 Jan 2014 by OriginalGriff
Perhaps it would help is you added the new row to the table?DataTable.NewRow[^] just creates a new blank row with the correct schema / columns...
13 Jan 2014 by Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)
You need to bind the DGV again like below.private void addNewRowButton_Click(object sender, EventArgs e){ csvData.NewRow(); // Provide Column values for the New Row. DataGridView1.DataSource = csvData; DataGridView1.DataBind();}
7 Dec 2011 by ckulasekaran
Hi, How to add particular column values in datagridview. Example: Add 5th column all values and display into a text box.Front end is vb.net and back end is ms access.
7 Dec 2011 by coded007
These both links helps you...http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/a4d1c74d-1419-4946-9697-6171e63aec02[^]http://social.msdn.microsoft.com/Forums/en/winformsdatacontrols/thread/893c0701-461e-40f2-b941-622ecbc94c1f[^]
30 May 2020 by raigen
I created a datagridview PlanningDataView that binds the value of the database, I'm using ADO.NET database model private void MainDashboard_Load(object sender, EventArgs e) { int numberRow = 0; var planningList...
30 May 2020 by RickZeeland
As you are using planningList as the binding source, you should increment the planningList.ID
10 Feb 2012 by Ravi Sargam
I Have DataGridView View like thisStudRollNo Name1 AAA2 BBB3 CCCI want to Add Columns to DataGridView of Subjects Table which is also comes after selecting Standard using Datatable to Existing columns like StudRollNo,NameI want Like This...
10 Feb 2012 by RDBurmon
As per my understanding below could be the implementation to achieve thissuppose you have two table as per belowTable1 : StudentRollNumberName1AAA2BBB3CCCTable2 : SubjectRollNumberEnglishMathsHistory150607024045503356538then you query will...
25 Jun 2016 by bhavikadb
hello,I Have a Table in Database with some columns and when I bind that table to Datagridview using dataset I want a Combobox and CheckBox to some Columns of Dataset and Others are Text/String. Also need to fire Events Of That Components. How can I achieve it?What I have...
25 Jun 2016 by RickZeeland
Here is an example that uses a BindingList, which can be adapted for database usage.The BindingList uses this class:public class dgvClass1 : ICloneable{ /// /// priority is not a propery, so it is not visible in datagrid by default. /// public...
19 Aug 2011 by version_2.0
Hai all,how can i do this "Adding data from server To DataGridView while scrolling DataGridView".ORAdding Data to DataGirdView Asynchronously..?Please Help me..
24 Aug 2011 by Tejas Vaishnav
Hello friend...in your case you need to write a sql query or store procedure that will return only some rows as per it will execute.. like paging first time this will return 1to10 then 11to20 its up to you, how much row you want to display in your grid...then you have to attache an mouse...
7 Sep 2017 by Kasthuri Gunabalasingam
Window have 2 text boxes ,one button and one Data grid view . when click button , data grid view need to display values of text box!What I have tried:private void btn_Click(object sender, RoutedEventArgs e) { DataTable dt = new DataTable(); ...
3 Aug 2016 by Animesh Datta
Hello, Every time you are generating new object of DataTable in button click and then checking whether it contains any row or not . Obviously it does not contain any row. That's Why data is not showing . First declare the Table globally means out side of button Click and define its...
14 Mar 2012 by Ravi Sargam
I Have a datagridview like thisid name age-----------1 AAA 222 BBB 233 CCC 334 DDD 44I want to add id values to an int array on click of id cell..thx in advance...
14 Mar 2012 by ProEnggSoft
I think it will be easy to add the IDs to a list. However, if you want an array ToArray() method of Listclass can be used.List IdList = new List();private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e){ ...
14 Sep 2013 by CAS1224
I've been able to change an individual datagridview cell to a combobox in my vb.net windows forms program. Now I'm hoping to find a way to change the entire column to a combobox. Here is my code to change the individual datagridview cells to combobox, the same approach should work but I haven't...
14 Sep 2013 by Jake_L
Would something like this work for you?For cell As Integer = 1 to n Dim myComboBoxCell As New DataGridViewComboBoxCell DataGridView1(3, n) = myComboBoxCell 'Change as neededNext cell
13 Feb 2014 by dash2020
Hi allI have a datagrid where I am showing contact list of our people with their individual email address/s. I have added a checkbox.I want to check if outlook new message is open if it is open then add all selected email address (datagrid cell value) to the BCC field.Here is my...
15 Oct 2022 by Member 14861076
I want to print a list of students in a class. My columns are things like admno,sname,gender and class(e.g form 1B,1G,2B,2G etc). The code I use only print the available values from a dgv using the rectangle object. To draw the rectangle and...
15 Oct 2022 by Gerry Schmitz
You're already printing "rows and columns" using existing values. Put the (for loop) logic in separate methods that you can call with parameters. In that way you "add" extra / custom rows and columns (using "blank" parameters for extra empty rows...
2 Dec 2014 by lukeer
Hi folks,user shall be able to edit a list of these cute little demo objects:class Foo{ public int FirstCol { get; set; } public int SecondCol { get; set; } public Foo(int first, int second) { FirstCol = first; SecondCol = second; ...
13 Dec 2014 by Anisuzzaman Sumon
As a result of googling a lot I've come to a decision that actually there is no way to add new row to a datagridview which has a binding source.However I am not sure at all.But According to your need I am giving you a solution I think it works for you fine.Please check it.Thanks ...
14 Dec 2014 by lukeer
The solution is to add a parameterless Foo constructor.class Foo{ public int FirstCol { get; set; } public int SecondCol { get; set; } // This c'tor is needed for the user // to add entries from within // a databound DataGridView. public Foo() { } ...
4 Sep 2019 by Member 14532396
I have defined this datagridviewcombobox: Dim lCol2 As New DataGridViewComboBoxColumn lCol2.DataPropertyName = "Descrizione" lCol2.DisplayMember = "Descrizione" lCol2.ValueMember = "IDTessera" lCol2.DisplayIndex = 2 dgwNote.Columns.Add(lCol2) ...
4 Sep 2019 by Gerry Schmitz
DataTable.NewRow Method (System.Data) | Microsoft Docs[^]
4 Oct 2011 by kals84
Hi,I am working on VB.Net Windows ApplicationI have a datagridview with two column. 1- Text and 2- Image.I have add a image "someimage.png" in resources and same in imagelist.when i add new items at runtime to datagridview the image dows not show.'adding the image with...
4 Oct 2011 by Abhinav S
See this[^]. It may help you.
15 Jun 2015 by Krunal Rohit
And you sure about this[^] ??-KR
20 May 2013 by naveu2007
Please help me in adding a new row(editable) in data gridview when user clicks on add button in the grid in WPF.And also update the same in the sql database after click on save button.I get the data populated in to the grid view using the SqlDataAdapter and Sqlcommand.
1 Nov 2015 by R.M49
I am trying to Add from textboxes to datagridview but the code doesn't workdataGridView1.Rows[0].Cells[0].Value = textBox6.Text; dataGridView1.Rows[0].Cells[1].Value = textBox5.Text; dataGridView1.Rows[0].Cells[2].Value = textBox7.Text; ...
1 Nov 2015 by OriginalGriff
Try:dataGridView1.Rows.Add(t...
28 Apr 2014 by Declan Bright
A technique which ensures that an ASP.NET GridView displays nicely on small screen devices.
18 Dec 2014 by Member 11284808
I have two datagridviews in one from. I need to get data from database to datagridview1 (using Select *from database...) then I want to add data from datagriwview to datagridview2 using Selected Rows.First I wanted to solve this problem to get Selected Row's ID, when I select row in...
18 Dec 2014 by Maciej Los
If i understand you well...The quickest way is to use Linq[^]://get selected rowsvar qry = from DataGridViewRow dgvr in DataGridView1.SelectedRows select dgvr;DataGridView2.DataSource = qry;Note: not tested! Just an idea.Second solution is here: How to: Get the Selected...
18 Dec 2014 by Member 11284808
It is another way I used it, but It did not work neither. How can I check if value already exists in datagridview2? When I select row from datagridview1 it shows in datagridview2, but when I select same row twice, it repeats ((((if (dataGridView1.CurrentRow.Cells["Id"].Value != null) ...
8 Jun 2016 by Itz.Irshad
How to add a row in datagridview in winform application.Scenario is: - I've one grid on windows form.- Grid's columns are pre-definded. (Here, I mean column index, name , header text and order is not changeable at runtime.)- I need to add new row in that grid.Anyone, help me in this...
10 Jul 2012 by Prasad_Kulkarni
Try this:private void InsertNewRowToDGV() { //we have to get the index of a last row id: int index = this.dataGridView1.Rows.Count; //and count +1 to get a new row id: index++; this.dataGridView1.Rows.Add();}Refer more...
10 Jul 2012 by K C Behera
Please this code given bellowto add blank row into the data grid viewDataGridView.Rows.Add().Or Can you use this coding alsoSuppose I have a DataGridView having three columns named ItemName,Itemcode and ItemQuantityfor that you can use following code given...
20 May 2010 by HennieSmit
Hi I am creating a WinForm app and one form I have a dgv which is databound to a SQL database. I have almost got it figured out how to programatically add more roes to the dgv, but have some obstacles to overcome. The code below is from the event where everything happens. It reads the...
21 May 2010 by neal123
Hi,Have you checked NewRow as below-for (int i = 0; i
21 May 2010 by HennieSmit
I have changed the last portion of the event as follows: DataRow drARR; this.fp = dtUniqueForex.Rows.Count; for (int i = 0; i
22 Feb 2012 by Ravi Sargam
I Have dgv with Rollno,StudName columns and DataTable dtStud which returns data like thisStudid Name Rollno StandardId1 AAA 1 12 BBB 2 13 CCC 3 1I want add this data to dgv(DataGridView) columns Rollno,StudNamethx in advance.
11 Aug 2015 by ÃHmed Élkady
i create a new dataset by click on add new item and new dataset and crate table called itemname and i want add the datagridview column 1 to the datatable ut nothing connected to database sow how i can make that to mak crystal report from th datagridview
11 Aug 2015 by George Jonsson
You need to connect the DataSet to the DataGridView.To do that you set the DataGridView.DataSource property.Normally you first create a BindingSource and connect the DataSet to the DataSource property and then connect the BindingSource to the DataGridView.DataSource property.See...
3 Feb 2015 by ChrisCreateBoss
I have a DataGridView with 6 columns; I want the first column to display the days of the week. And the second column should display a schedule per day. But I want them to display in vertical order, not in horizontal way. But I can't figure out how to do it.I have it like this:...
3 Feb 2015 by ChrisCreateBoss
I finally solved my problem, using this code:dataGridView1[2, 0].Value = "7:00 - 7:50"; dataGridView1[2, 1].Value = "7:50 - 8:40"; dataGridView1[2, 2].Value = "8:40 - 9:30"; dataGridView1[2, 3].Value = "10:00 - 10:50"; dataGridView1[2, 4].Value...
8 Aug 2011 by aslam.kship
I have a datagrid in my form.in addition to the datasource im using, i want to add additional columns on a button click.these coloumn cells contains same text (string) in every row. i know how to add column and textboxcell. my below code does that well.but i dont know how to show some...
8 Aug 2011 by Herman<T>.Instance
foreach (GridViewRow row in dataGridView1.Rows){row.Cells[row.Cells.Count-1].Text = "EDR";}
8 Aug 2011 by Praveen Kullu
You can easily do it with the copy method of datagridviewprivate void button1_Click(object sender, EventArgs e) { dataGridView1.Rows.AddCopies(0, 1); //row =0, no.of copies =1 }
13 Dec 2012 by wkiess01
I have a DataGridView control named uxContactEmailAddressesGrd. It is data bound to a collection of email address objects. It has 3 columns: 1) Hidden: email Primary Key (PK) (int) 2) shown: email address (string) 3) Shown: email type (combobox, ValueMember=int,...
14 Dec 2012 by M.Edmison
These are just thoughts to work with and may not be the full answer.You might want to look at something around EditMode property instead of "AllowUserToAddRows" for update capabilities.Might be parted with IsCellInUpdateMode and/or on click events to avoid the first char getting ignored,...
17 Dec 2012 by wkiess01
I've had very little response to this question on this forum and others. It must be a very difficult problem, or I'm simply expecting too much from the DataGridView control.After many hours of trial and error, I have settled on a solution which does not require subclassing. The solution is not...
19 Oct 2014 by vivek murli
I have written the following c# code to load the contents of the .DAT file to ListBoxCode:[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)] struct file_record { public char Exch; public int ExchCode; public byte NameLength; ...
20 Oct 2014 by V.
You have to go through a DataGridViewRow[^]. The DataGridView has a property "Rows" that has a function Add( ... ).In order to use this "manual" way of adding rows you cannot set the DataSource propery. If you did set the DataSource property you need to change the DataSet, Table, ......
21 Jan 2016 by Shrikant S
I have a user control which I want to show in winform DataGridView as DataGridViewColumn. Can anyone help me please ?
27 Apr 2012 by PointBreaker
In my C# windows form program, i have two forms: 1 is Form and the other one is Form2the Form is where I put data on the textboxes and the Form2 is where is a datagrid wich shows a table with the data i inserted. Now, how do I put data from a table to a form?My code on Form2namespace...
27 Apr 2012 by VJ Reddy
I think something like this can be done.In Form a DataTable say DataTable1 is created and it is populated with the values from the TextBoxes.In Form2 make the visibility (accessibility) of DataGridView as public.Then in the Click event of Button1 of FormForm2 form2 = new...
4 Aug 2015 by ÃHmed Élkady
while i was searching in google i find a code to add the items from datagridview to database using c# for (int i = 0; i
4 Aug 2015 by Richard Deeming
Using a properly parameterized query will fix the SQL Injection[^] vulnerability in your code, and also fix the error you're seeing:using (SqlConnection connection = new SqlConnection("-YOUR CONNECTION STRING-"))using (SqlCommand command = new SqlCommand("INSERT INTO Bills (ItemName)...
12 Sep 2011 by Joanne0005
Greetings, I would like to know what's wrong with my code. What i want to do is add the inputted datagridview records in the database when I clicked the ADD button. But I'm getting an error (included in the remark), pls help me I need to finish my thesis on Friday, thank you very much.da2 =...
12 Sep 2011 by Joanne0005
Thank you very much prdshukla, i get it now and it's now working properly. Here's the changes that i made:da2 = New OleDbDataAdapter("SELECT * FROM PurchaseDB2", conDB) da2.Fill(ds2, "PurchaseDB2") ds2.Tables("PurchaseDB2").Clear() ' i added this Dim newRow2 As...
6 Feb 2013 by Prathap Gangireddy
Hi All,I have two forms Form1(DGV1) and Form2(DGV2) with Datagridview control on both the forms.On Form1 there is a button link to Form2 where user can add rows to Form1 datagridview(DGV1)..In Form2 Datagridview there is a checkbox in the first column and the user can select only one...
7 Feb 2013 by Prathap Gangireddy
Hi ,I have solved the problem.ItemID is a unique column in the grid so used that to check whether an additional row is added.objQM.gvItemDetails.Rows.Cast().Count(c => c.Cells[7].EditedFormattedValue.ToString() == ItemID) > 0)one more problem is..Suppose I have...
30 Oct 2015 by R.M49
I Have form 1 with 4 textboxes and 1 button, and I have form 2 which contains dataGridView, I want to add new row to the datagridview from the other form.but it give me this error:Index was out of range. Must be non-negative and less than the size of the collection.Parameter name:...
30 Oct 2015 by VR Karthikeyan
Hi, Add Columns in your datagridview, because without defining columns you can't assign data to datagridview, so create columns in datagridview, and use the following line to add rows to datagridview.frm.dataGridView1.Rows.Add(textBox1.Text, textBox2.Text, textBox4.Text,...