Click here to Skip to main content
15,889,335 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 Updated

DataGridView 

1 Apr 2024 by Member 15485980
I have a VB windows form app with a datagridview. I changed one of the column types to a combobox, which works ok. However, when I close out of the form I get multiple 'datagridview default error dialogue' complaining about system.index out of...
29 Feb 2024 by OriginalGriff
We can't tell - we have no access to your code, especially not while it is running. So, it's going to be up to you. Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how...
2 Feb 2024 by OriginalGriff
Spammer target question: 16 or so of them this morning. I'll delete the question as after 8 years no-one will be interested in getting a solution now.
8 Jan 2024 by OriginalGriff
We can't tell - we can't run your code with your data and see what is happening. So, it's going to be up to you. Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to...
8 Jan 2024 by Member 14200176
let guess datagridview loaded 10 data. When i clicked SelectALL 9 row checked true but first row not checked false. even if datagridview loaded only one data then i clicked SelectALL but row not selected What I have tried: private void...
28 Nov 2023 by Andre Oosthuizen
The Format function you used in your code in VB is generally used for formatting strings, not for converting data types. you should convert the formatted string to the appropriate data type before assigning it to the cell. Assuming 'Amt' is...
27 Nov 2023 by Richard MacCutchan
You have defined that columns an an Int32 type, so you can only assign an integer value to it.
27 Nov 2023 by mmaucher
Trying to assign a string "$57,834" to a DataGridViewTextBoxColumn "Amt": For Each dgr As DataGridViewRow In dgvExpSubject.Rows dgr.Cells("Amt").Value = Format(amt, AG.MASK_DOLLARS) Gives this error: DataGridView Exception: $57,834 is...
22 Nov 2023 by Mike Hankey
Capture the TextBox click command and keep track of how many times the user clicks on the TextBox. The TextBox class is here[^] Since you have several TextBoxes I would use the Tag property to keep track of number of clicks.
22 Nov 2023 by Rusty25
I have a datagridview and some of the columns there are textboxes within the grid. I want to code it so when user clicks into the textbox it populates the text with a hardcode string value1, if they click on it twice then it populates with...
8 Nov 2023 by CHill60
Quote: That's exactly what i was waiting for . A nonsense comment. thanks Not really nonsense though is it? As Maciej says in his comment, it's "unfriendly to the UI" and Gerry says "without smashing the keyboard". At the very least it's...
6 Nov 2023 by Ankit Goel
I have a textbox and a datagridview with some results. I am looking for a way in which if the user is typing something in the textbox and in between, he uses his mouse and clicks on the datagridview, the focus should not leave the textbox means...
1 Nov 2023 by Devendra Sarang
I want to achieve 2 functionalities as shown in the following pic: Want to achieve this At the top, there is a dropdown box and which contains values like 5, 10, 20, 50 and on changing it, it should display number of records in datagridview as...
31 Oct 2023 by M Imran Ansari
Implementing pagination in a DataGridView in a C# Windows Forms application involves managing the data source and updating the DataGridView's content accordingly. To implement the additional features you've mentioned, you'll need to modify the...
3 Oct 2023 by Marco Giglio
I created this code (I'm only showing part of it) where I read an XML file and display it in a datagridView. In this, the attribute (table:style-name="ce215") changes the background of the single cell of the datagridview to yellow, but I can't...
2 Oct 2023 by Graeme_Grant
This is a common question with many answers on the internet. A simple Google search will give you many solutions: winform datagridview cell background color - Google Search[^]. Also there are related questions with solutions to the right of this...
15 Sep 2023 by Member 11408361
I'm completely new to VB.NET and have been given a homework assignment. I need to be able to read certain lines and display them in a DateGridView. I have been able to link my notepad .txt file to the DGV, however it reads the whole file as...
19 Aug 2023 by headshot9x
I have a class object and the Datagridview. I want to convert data in Datagridview to object, and can get/set values in object. Assume the quantity element in Datagridview equal to field of object, and variable name like the name first of...
19 Aug 2023 by OriginalGriff
Bear in mind that your processing is in a loop: so if your DGV has multiple persons, only the the last will be used - the earlier data will be discarded. You can't access _person in your button click handler: it is local to the GridviewToObject...
8 Aug 2023 by PaulaJoannAllen
I am using DataGridView for displaying Excel spreadsheets, all of that is fine until I got to the sheet that contains a bar graph as well as some data. I cannot figure out how to display the graph and need some help. What I have tried: ...
8 Aug 2023 by Dave Kreskowiak
You can't. The DGV is not Excel. It doesn't support showing any graphs like Excel does. You'll have to use a charting library and feed it the data to create a new chart in your code/app and show that.
8 Aug 2023 by Member 12335655
what i have: data in GDV (column1, Column2) aim: link all data present in dgv to the chart, displaying the line graph problem: can't load the graph What I have tried: Public Class Form1 Private Sub Button1_Click(sender As Object, e As...
3 Aug 2023 by Member 12561559
Hi, So I have a datagridview, it has cellformatting and depending on whether there is a value in the bindingsource, will put an image in the cell. All good so far. Now, the background selection colour (color for the Americans) has been changed...
3 Aug 2023 by Member 12561559
Found it. What an amateur mistake. In the cellformatting, I can just check if the e.rowindex is selected. Doh ! If DataGridView2.Rows(e.RowIndex).Selected = True Then If Int32.Parse(e.Value.ToString) = 1 Then ...
31 Jul 2023 by JOEY VERANO JR.
Hello everyone, hoping to answer my question. I am trying to rank group of scores in a datagridview containing 4 columns. I want the cells of the column B to have values as 1, 2, 3, based on the values of Column A. And for the Column D, I want...
31 Jul 2023 by Maciej Los
Well... I'd suggest to work on data instead of datagridview cells. Here is an idea: Sub Main() Dim numbers AS Integer() = New Integer(){8, 9, 1, 10, 11, 2, 12, 13, 3, 3, 5, 14, 15, 16, 17, 18, 8, 8, 8, 8, 19, 19} Dim result As List(Of...
12 Jul 2023 by LiterallyGutsFromBerserk
I have a datagridview that looks like this : https://i.stack.imgur.com/0fRKb.png[^] and I want to resize my table inside so that it fits it exactly. Do you have any idea on how to do it? What I have tried: I tried this: ...
12 Jul 2023 by OriginalGriff
That's because as the name implies that only affects Columns. Rows are different: under normal circumstances they are all the same height, and if there is too much data to fit in the column width the data is truncated and an ellipsis added to...
8 Jul 2023 by LiterallyGutsFromBerserk
Hello everyone, I want to merge some of the rows in my datagridview and I'm using this article. When I load my datagridview, I want to change the type of my columns so that they can have the "rowspan" and "colspan". How can I do it? What...
8 Jul 2023 by Sandeep Mewara
Posting OP's solution to get it off unanswered list. OP resolved it by: Quote: I managed to get unstuck by copying the columns I wanted to edit to new columns, and then removing the old. Works like a charm!
6 Jul 2023 by LiterallyGutsFromBerserk
I made a datagridview for a project of mine, which is displayed as in the picture below : https://i.stack.imgur.com/JAjAs.png[^] The columns on the left are generated from my database, while the ones on the right are added dynamically (they...
6 Jul 2023 by LiterallyGutsFromBerserk
I'm currently working on a project wheere, for each category and subcategory of my products, I need to do some inspections. Here's how my winforms works: 1.I click on the first combobox which indicates categories : ...
6 Jul 2023 by LiterallyGutsFromBerserk
Hello everyone, I am using the ClosedXML library in c# for my WinForms datagridview, and in my code, I added a bunch of columns dynamically. My issue is that those columns aren't exported afterwards to my excel(my guess is that it's not in the...
4 Jul 2023 by LiterallyGutsFromBerserk
As OriginalGriff said in their comment, the solution to this issue is to simply edit the formatting from the cellformatting method in order to avoid changes being overridden. In my own case, I declared an global integer "numColsGen"(number of...
1 Jul 2023 by Ankit Goel
Hi , i have created a custom datagridview and trying to move my cell to the right when i presses enter key (instead of going to down on pressing enter key ) but i found that i have to press enter twice to move rightwards . when i presses enter...
1 Jul 2023 by Graeme_Grant
Microsoft Excel moves the cursor down a row if the enter key is used. I would recommend using this as it is familiar. However, to move the cursor right, not down, on using the Enter key, it is possible. The following code will wrap the cursor...
1 Jul 2023 by OriginalGriff
The question you should be asking is "why am I trying to subvert a standard mechanism that users are familiar with?" You are having problems because the actions associated with ENTER in Windows apps is handled by and large by the system: it is...
26 Jun 2023 by OriginalGriff
First off, never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead. When you concatenate strings,...
25 Jun 2023 by Sh.H.
I guys. How can I make a table in WPF VB.NET? I searched a lot in the web, but I can't find a good answer. What I have tried: I thought to add a table, I should use Datagrid. Datagrid is perfect answer if I use in WindowsForm. But obviously bad...
25 Jun 2023 by RickZeeland
Take a look at Galador.ExcelGrid on GitHub written by our distinguished CodeProject member Super Lloyd. Although it is written in C#, you can probably use the dll in your VB.NET project.
24 Jun 2023 by Sh.H.
I wrote a simple wpf datagrid. So when I double click on a cell, the program crashes. I should add that I checked this post: https://stackoverflow.com/questions/19334326/wpf-datagrid-edititem-is-not-allowed-for-this-view-exception[^] But there...
24 Jun 2023 by Richard MacCutchan
This is the same issue as you already posted at Error on double click on datagrid in WPF VB.NET[^]. I have just built the program from your original code, and the corrections provided by @RichardDeeming, and it works correctly. If you still have...
23 Jun 2023 by Graeme_Grant
Recommended method of doing work from C# was given here: Export datagridview to excel while keeping the formatting[^]: 1. Create a Macro recording of the actions that you wish to perform 2. View the VBA code generated for you 3. Use the VBA code...
21 Jun 2023 by LiterallyGutsFromBerserk
I managed to find a solution with a PaintCell override : private void dataGridView2_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { if (e.ColumnIndex == 1) { ...
21 Jun 2023 by LiterallyGutsFromBerserk
I have a datagridview that's already bound to a database. For each row in my datagridview, it shows a minimal, avg/nominal and maximum value. However, I'd like it if there is only one value that I give it, to merge cells into one cell, just like...
21 Jun 2023 by diggudg
I want to use DropdownList in form view in asp.net web application to insert data from list to database. I tried using Please any body can give me code for this to insert data from dropddownlist to database using formview or by manualThanks
21 Jun 2023 by OriginalGriff
This is the same question you pasted this morning: Change a single cell's border style in datagridview[^]
21 Jun 2023 by Graeme_Grant
Google search is your friend: winform datagridview merge cells - Google Search[^] First search result: How to Merge DataGridView Cell in Winforms - StackOverflow[^] The default control is limited in what it can do. You will need to use a...
20 Jun 2023 by Richard Deeming
I was able to reproduce the problem. As far as I can see, it's due to the fact that you're adding items directly to the DataGrid's Items collection. If I change your code to add the employee to a List(Of Employee) instead, and assign that list...
20 Jun 2023 by cmarcotte
According to this page : Specify the Edit Mode for DataGridView Control - Windows Forms .NET Framework | Microsoft Learn[^] You must have Quote: The ReadOnly properties of the cell, row, column, and control are all set to false.
9 Jun 2023 by SamuelDexter
hello everyone. I am trying to rate a group of numbers in a datagrid containing two columns. I want the cells of the Coulmn B to have values as 1st, 2nd, 3rd etc, based on the values of Column A. For example: Col A Col B 80 2 305 1 23 ...
9 Jun 2023 by Member 15876310
Dim oDict As Dictionary(Of Integer, Tuple(Of Integer, Integer)) = DataGridView1.Rows().Cast(Of DataGridViewRow).GroupBy(Function(r) r.Cells(10).Value).OrderByDescending(Function(g) g.Key) _ .Select(Function(grp, index) New KeyValuePair(Of...
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 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...
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...
30 May 2023 by Ankit Goel
I have developed a customDataGridView for my project. In its OnEnter event , i have tried to set the first cell of first row and first column in editable mode as soon as it gets focus . It is declared as follows. public partial class...
29 May 2023 by OriginalGriff
It's the BeginEdit call - it causes a second OnEnter because the now-editable cell gets the focus instead of the whole control, and it's SetFocus that triggers OnEnter, I believe. Can you prevent it? I don't think so, it kinda makes sense in the...
25 Apr 2023 by Member 13702784
private void cmdSave_Click(object sender, EventArgs e) { //dbcon.Open(); try { dbcon.Open(); SqlCommand chkID = new SqlCommand("Select ReceiveID from PolicyReceive where...
25 Apr 2023 by OriginalGriff
You need to have two tables: one for the info that is common to all policies for a customer (or whatever the policies relate to) which has a unique ID row, and a second one which has a separate ID and a FOREIGN KEY which links back to ethe other...
18 Apr 2023 by Member 11400059
Hi in my C# windows form project, My datagridview have one ComboBox column. this combobox items read form database and each Row Combobox have different Items. But all of the Row combobox always have same items. how can fix this? please help. my...
18 Apr 2023 by Richard MacCutchan
foreach (var item in list) { categoryName.Clear(); You are using the same list for each row of the DataGridView, so each combo will contain the same data, which is the last set that you process. You should create a new list at the start...
18 Apr 2023 by Richard Deeming
You've declared the categoryName list outside of your loop. Every row is bound to the same list of strings, and you add the categories for all rows to that one list. Since you clear the list at the start of each iteration of the loop, the list...
31 Mar 2023 by Co. Aden
I have a DatagridView.I have set its DataSource From a Class. taskerEntities te = new taskerEntities(); var OMsMasterDescriptiveIndicators = te.MyTable.Select(x => new lccls {Id = x.Id, name = x.name }).ToList(); MyGrid.DataSource = OMsMasterDescriptiveIndicators;My Class is...
31 Mar 2023 by Nima MirzaAliKhan
maybe this is old but: @A_Griffin yes this way is better and there is no problem with making it visible again (@Chase Viking). Example: var item = dgv.Rows.Cast().FirstOrDefault(c => c.Cells[x].Value?.ToString() == X); ...
22 Feb 2023 by robo.pavlik
Good morning. Currently I'm rebuilding one application and I don't understand one thing. Program read data from SQL and write this data to datagridview. Everything is OK, only last penultimate 8 records is not written to datagridview. What can...
22 Feb 2023 by OriginalGriff
At a guess, it's the RECORDS_COUNT that's too small - but we can't see where that is set. If it's too small, the final record will be overwritten multiple times and you will get a difference between the DGV rows and the printed output. Use the...
20 Feb 2023 by card_maruis
Hello my project has a datagridview with 3 columns that are completed manually by the user and a database table with 100 records i need to delete from database table the records that are like the first column(Serial) of the datagridview ...
20 Feb 2023 by 0x01AA
Apart from, that your delete sql is wrong I see no problem. But it is curious that you have success deleting the last row ;) The delete statement should be as below, notice 'FROM' DELETE FROM Articol WHERE Serial = @Serial" I hope it helps. ...
20 Feb 2023 by raddevus
I believe your delete syntax is incorrect. Not sure how it even deleted the one record. Should be : (You forgot the FROM) "DELETE from Articol WHERE Serial = @Serial" And, oh yeah, I'm assuming the table name is Articol. The syntax of delete...
6 Feb 2023 by OriginalGriff
Try something like this:SELECT a.LEADER AS [A Leader] ,b.LEADER AS [B Leader] ,c.LEADER AS [C Leader] FROM TableA a JOIN TableB b ON a.ID = b.ID JOIN TableC c ON a.ID = c.ID And also read up here: SQL Joins[^]
6 Feb 2023 by Joel Silva 2022
I have a column named LEADER in 16 tables in my sql database I'd like to join all the tables to show the Leader column and the table it belongs to in datagridview. What I have tried: SqlCommand cmd = new SqlCommand("Select...
24 Jan 2023 by Choroid
Here is what I did wrong I started a new C# project and created a form then I opened a VB.Net project and copied a DataGridView (DGV) that was on a form and pasted it into my C# form. Populated the DGV from the SQLite DB great it worked....
24 Jan 2023 by Choroid
OK Dave Kreskowiak I then believe the reason is because I copied and pasted from a VB.Net application? I did test by using the DGV under All Windows Forms. in the C# application. And it would not read the click event Thanks for the Insight and...
23 Jan 2023 by Dave Kreskowiak
Quote: For anyone reading this POST I am guessing that you need to use the DGV under All Windows Forms for VB.Net and for C# you need the DGV under Data. No, you are wrong. The various tabs in the toolbox are merely for organizing controls into...
15 Jan 2023 by Graeme_Grant
Okay, I have a sample app to show you how to filter. It is based on this: BindingSource.Filter Property (System.Windows.Forms) | Microsoft Learn[^] I have turned on VirtualMode and created 35,000 rows to reproduce your situation. Form...
15 Jan 2023 by Houman Farokhi
i have a datagrideveiw with about 35000 rows , and i want to filter it by a textbox. i wrot this code: void txtSearch_EditValueChanged(object sender, EventArgs e) { BindingSource firstBs = new(); firstBs.DataSource = dgvIcd11.DataSource;...
31 Dec 2022 by Graeme_Grant
As I am unfamiliar with how to print a Datagrid using ProtDocument, I did a quick google search: c# print datagridview using printdocument - Google[^]and there were lots of solutions - first 3 results: * C# DataGridView Printing[^] * How To...
29 Dec 2022 by OriginalGriff
You don't pass a string to Graphics.DrawImage Method (System.Drawing) | Microsoft Learn[^] - there isn't an overload that takes one. Instead, you need to get the Image that the cell contains and pass that instead.
17 Dec 2022 by Member 15771242
[SOLVED]After days of searching, I found this solution obviously being new to this field, it's an inelegant solution but it works. However I will look for a better solution. Hope it helps someone. private void btnPrint_Click(object sender,...
17 Dec 2022 by Member 15771242
I have 15 columns in dataGridView1, I would like when I print, it shrinks to fit all on one page. I would like the 15 columns to be seen all on one page while as it is, one column is outside and is printed as a second page. How to fit all the...
14 Dec 2022 by Michele Signorile
This solution doesn't work as there is no Fit To Page (printer.FitToPage) property for DGVPrinter, would you please show me how it worked for you?
10 Dec 2022 by CodeGuru84
To fit all the columns of a DataGridView on a single page when printing, you can try setting the FitToPage property of the DGVPrinter object to true before calling the PrintPreviewDataGridView method. This will cause the columns of the...
22 Nov 2022 by kudim
Hello I have dvg which is for category and make some calculations in every row like this [ Quantity * Price - Discount = Total ] and I want when I enter a value in every cell total Cell automatically changing to the new value I will attach codes...
20 Nov 2022 by RickZeeland
See answer here: DataGridView Auto-calculate cells[^]
15 Nov 2022 by Mushaik Mohamed
In my c# application i have to load a csv file to DataGridView and in my csv file have single quotes in person ID( ' ) how to remove single quotes from datagridview ('), can anyone help me please? What I have tried: String name = "exx"; ...
14 Nov 2022 by OriginalGriff
You would have to process the data and remove it manually from the DataTable once it was filled: either that or list the columns you want to select, and use Access's RIGHT function to remove it before it reaches the DT: String functions and how...
6 Nov 2022 by Cruz Vince
Here is the answer for (int i = 0; i
6 Nov 2022 by Cruz Vince
I have a column named Status inside a datagridview, now every row has a status of 1, i want to update them to 2. Here is the code i use private void btnCommit_Click(object sender, EventArgs e) { using (DataClasses2DataContext db = new...
27 Oct 2022 by NPetey
I am using VB.Net Windows form with a Datagridview and a retrieve button. The form is used by end users who paste job number numbers in column 0, then click the retrieve button and the data from the database will display in the grid. The code I...
27 Oct 2022 by Dave Kreskowiak
Are you sure that SQL statement is good and returning the proper rows? Somehow, I doubt it. This is what it looks like when you properly format the SQL and capitalize all the keywords: SELECT U.SearchKey , pj.LOCATION_CODE ,...
15 Oct 2022 by Hu Christian
Hi, I have a big DataGridView, and for some reason I need to save the value of each cell as tag. Now I use a loop to complete each cell one by one, but it wastes a lot of time because the DGV is large. Is there any way to speed up this work?...
15 Oct 2022 by OriginalGriff
No, not really. To set each DataGridViewCell.Tag property you have to visit each and every Cell in the DataGridView individually, and copy the Value property to the Tag property. Which is going to take time, with a big DataGridView. And is a...
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...
8 Sep 2022 by jbm417
My app uses EntityFrameWork 6.x in VS 2019 Ver 16.11.18 I bind a datagridview control to a bindingsource expecting all will be good. Problem is the grid allows user to enter more characters than the database table column allows. When this...
8 Sep 2022 by RickZeeland
You can find some suggestions on CodeProject here: How to set max length of datagridview column?[^]
30 Aug 2022 by dejf111
Hi, I would like to set the form so that its size changes depending on the size of the DataGridView. After that, I would like to set the button to always be at the bottom of the form and at the same time in its middle. What I have tried: Does...
30 Aug 2022 by OriginalGriff
There is no automatic way to change the size of any container to fit it's content: you would have to do that yourself. And it's gonna be messy: first you have to resize the DGV to fit the number of rows / columns it contains - and that's going...