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

30 Sep 2016 by #realJSOP
0) Create a model for your items1) Load the results of the database query into a list of model items2) Create a new model item that contains the desired summed fields (use LINQ to get your sums).3) And the new model item to the list4) Bind the list to the dataGridView control.
7 Oct 2016 by #realJSOP
Did you put a breakpoint on the first line of the tbl_alunos_subDataGridView_CellEndEdit method, and step through the code to see what's happening?I would also include e.RowIndex in the if statement to make sure you're saving the row that the edited cell was on.
18 Oct 2016 by #realJSOP
private static void EnsureVisibleRow(DataGridView view, int rowToShow){ if (rowToShow >= 0 && rowToShow
3 Jan 2017 by #realJSOP
Try this (I found it with google). If you don't mind the fake news filtering, it's agreat place to get code.(dataGridViewFields.DataSource as DataTable).DefaultView.RowFilter = string.Format("Field = '{0}'", textBoxFilter.Text);
6 Feb 2017 by #realJSOP
If I understand your question, you want to skip rows that have an "M" in a certain column.Change your if statement from this:if ((Myrow.Cells[8].Value.ToString()) == "M"){ //What should I do here?}else{...to this:if (Myrow.Cells[8].Value.ToString()) !=...
23 Oct 2018 by #realJSOP
You don't need to do two data fetches. You should use MVVM 5to accomplish what you want. 0) Create a model object that is responsible for getting data from the database (or other data source). 1) Create a viewmodel object that takes your model object, and adds properties that reflect the...
14 Mar 2019 by #realJSOP
System tray notifications are typically modeless so that they don't interfere with apps running on the desktop. To set a cell's background color, do something like this: DataGridViewCellStyle style = new DataGridViewCellStyle(); style.BackColor = Color.FromArgb(255,0,0); style.ForeColor =...
7 Oct 2019 by #realJSOP
0) As already noted, don't do it that way. 1) Why in the hell are you using a Winforms control in WPF? Just because it "can be done", doesn't mean it should be done. 2) Use the MVVM pattern, and perform appropriate binding(s). You should be able to move your foreach loop into your observable...
9 Dec 2021 by #realJSOP
0) Instead of providing the filename like you're doing, create a string constant set to the filename, and then use the filename constant. Doing this will make it possible to change the filename in one place and not worry about whether or not you...
16 Feb 2015 by /\jmot
dgv.Rows.Add(10) Not gonna work here.see the references here.Ref.http://forums.asp.net/post/2165946.aspx[^]http://geekswithblogs.net/dotNETvinz/archive/2009/03/11/tiptrick-show-header-and-footer-of-gridview-when-no-data.aspx[^]
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. ...
8 Apr 2022 by 123 123 2022
I extracted values from datagridview1 to calculate an exponential equation. foreach (DataRow pr in Global_var.data_source.Tables[0].Rows) { try { x1 = (double)pr[0]; y1 = (double)pr[1]; ...
8 Mar 2022 by 123 123 2022
How to empty specific column of numbers from datagridview? (i mean erase the values without header, not delete the column). ERASE datagridview calumn values, so i dont need to do it manually and continue writing new ones. How i created the...
19 Nov 2013 by 123ugur
Hi Everyone. I need you help. I have a two form. there is a Datagridview in Form1 and there is 5 units textbox in Form2. My question is: when i selected cell click in datagridview, How to Pass Values Form2 textboxes
5 Jul 2012 by 2irfanshaikh
I think this will help you:http://stackoverflow.com/questions/9905051/is-it-possible-to-check-cell-value-length-in-datagridview-while-typing[^]
15 Aug 2014 by 4L4K1
when I want to show a dataGridView with backgroundWorker I call showDGV("mydataGridViewname") in bw_dowork function . delegate void Dstr(string s); public void showDGV(string s) { if (Controls[s].InvokeRequired) { Dstr h = new...
5 Mar 2019 by 5TY
Dears, Could you help me. I have a problem with the HTML table grab only the red and yellow alerts and put in the DataGridView. I tried first to grab a specific item with the help of Htmlaiglity Pack but it also does not work. Below I send HTML code and C# code
20 Apr 2019 by 5TY
Hello, I do not know and I try to sort datagridview. I want to sort by "Time". I have 4 columns with 3 columns in string and and 1 in DataTime. I want to sort column Date Below my code. var test = d.DocumentNode.SelectNodes("//td"); string patt =...
13 Dec 2012 by 90jeri
HI!i have a data grid view that has as datasource one data table of dataset. when i try to add a new row to datatable sometimes datagridview is not displaying any rows. when it happens, it does it just from the application startup(form shows) i mean that the datagridview does not display...
15 Oct 2012 by 9783444184
hi all,Can i bind data grid view with sql procedure in my procedure i want to use only cursoron any table...how to use please....thanks in advance
30 Nov 2017 by ________________
First form - DataGridView and button: public partial class Form1 : Form { public Form1() { InitializeComponent(); this.dataGridView1.DataSource = new List { "qwewqe", "qweqweqwe", "aaaaaaa" }; } private void button1_Click(object sender, EventArgs e) { Form2...
30 Jul 2012 by _Amy
Hi,You should read this:MSDN : DataGridView Control (Windows Forms)[^]MSDN : DataGridView.Columns Property [^]--Amit
16 Sep 2012 by _Amy
Right way of asking question.Add a MessageBox inside try block and check without putting a break-point. Whether the debugger is coming inside or not. Clean and rebuild your solution.Try this:try{ GridUtitlity.strFind = tsTxtSearch.Text.Trim(); MessageBox.Show("Hello"); ...
25 Feb 2014 by _Asif_
I guess Dynamic LINQ is the solution. Check below articles.Dynamic LINQ to join on dynamic columns on DataTable[^]Any way to dynamically choose fields in a LINQ query expression?[^]dynamic linq query to dataset[^]
15 Nov 2011 by _Damian S_
You need to use a join. They work like this:select studentname, [local address], from student inner join studentinfo on student.stuid = studentinfo.stuidInner join gives you records where there is s match in both tables. If there's a chance of no match in both tables, use a left join...
18 Jun 2013 by _Damian S_
If you are wanting to show all deliverables, regardless of the project chosen, you could, but you'd probably be better to have the deliverables show for the selected project.To show all deliverables (regardless of project selected), use a stored procedure something like this:select...
3 Jul 2013 by _Damian S_
Yes. Set a condition on your datatable, then bind it to your datagrid.You could also use a stored procedure/query with a where clause to achieve the same result.
24 Feb 2015 by _ivy
In my Windows Form I have 3 DataGridView placed within a TabControl. All I can do as of now is to export the DataGridView content to a new excel file with 3 sheets. But then I am also ask to append the content of each DataGridView to an Excel master file with 3 sheets as well. Any Idea on how to...
31 Dec 2015 by _Tuba
I'm trying to open excel file in datagridview ,the problem when I choose file doesn't show all file in datagridview ? OpenFileDialog op = new OpenFileDialog(); //tool to open the file op.Filter = "ALL Files |*.*|Excel Files |*.XLSX"; //select the type of file that want open...
4 Nov 2013 by A.Girish
Hello GuysI have problem in design Datagridview i want to design the datagrid with image and text as column header and image as status of that column in row cells.
30 Mar 2011 by a1mimo
HiFrom what I understood your problem is that you use the same dataset with the same TableAdapter and BindingSource in both the DataGridViews , if this is right then here is your solution you just have to make a copy of your dataset and TableAdapter and also the BindingSource lets assume they...
15 Apr 2011 by a1mimo
if you want the row ReadOnly property to be false then use thisdataGridView1.CurrentRow.ReadOnly = false;
29 Oct 2017 by A_Griffin
OK - well, if you haven't already, you must first setuserdatagrid.AllowUserToAddRows = False either in the form designer, or in the form load event. The put this in your new search button's Click event: Dim r As Integer, c As Integer Dim bViz As Boolean = False ...
20 May 2014 by aamitsengar
Use Tooltip Or a user control instead of grid..
12 Dec 2012 by Aarti Meswania
as below...dim First_selected_row_Ind = dataGridView1.SelectedRows(0).Indexdim Last_selected_row_Ind = dataGridView1.SelectedRows(datagridview1.selectedrows.count-1).IndexHappy Coding!:)
10 Mar 2013 by Aarti Meswania
reason isyou are getting...theRow.Cells[colIndex].Value is NULL and you are tring to use tostring conversion methodNull.ToString() that is error because NULL haven't a method tostring()so,...if ( ! theRow.Cells[colIndex].Value.Equals(Null)){ if...
3 Aug 2013 by Aarti Meswania
create a custom control inherit from gridview override below two methods. and use this custom gridview instead of datagridview.Protected Overrides Function ProcessDataGridViewKey(ByVal e As System.Windows.Forms.KeyEventArgs) As Boolean Try If e.KeyCode =...
16 Jan 2014 by Aarti Meswania
if you want that user can not insert any special char then handle it in cell_keypress eventand if e.key is special char then handle it using e.handle = true;Happy Coding!:)
17 Sep 2019 by Aarti Meswania
Hi kjohansen2000, please gothrough below steps, 'implement event "DataBindingComplete" for your dataGridView. In that... 'start a for loop for each row of grid... 'identify your targeted cell value in the row to write if else condition... 'if value is XX then...
4 Dec 2013 by abbaspirmoradi
this is not good idea my friend.just read this article.it have a method that you give your datagridview to this method and it print all column you want(Print_DataGridView).try this:DataGridView Printing by Selecting Columns and Rows[^]
18 Oct 2016 by abboudi_ammar
hi, my question is about the datagridview. I create a class ctsContacts with this source code:using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace TestingListe{ class ctsContacts:lstContacts { private int _numero; ...
24 Nov 2016 by abboudi_ammar
Hi, I'm trying to assign a dateTimePicker control to the datagridview cell as shown in this imageI find a solution similar to this problem in this site: http://www.c-sharpcorner.com/UploadFile/0f68f2/embedding-calendar-datetimepicker-control-into-datagridvie586/ But in this proposal it...
25 Aug 2021 by Abdelrahman-Dev
Hello, I have a C# winform application for selling books, And in my selling form I have some textboxes where the user can enter the name of the book, the amount, the price, etc... I have a button for inserting these values into an Access...
13 Nov 2013 by Abdullaziz Said
hello brothers i want to Fill textbox inside a DataGridView with data form database depended on selected value from combobox inside the DataGridView this is the code for filling the cobobox private void create_report_Load(object sender, EventArgs e) { ...
2 Mar 2015 by Abdulnazark
see the link & also set EnableEventValidation="false" to the formhttp://www.aspsnippets.com/Articles/Export-GridView-to-Excel-in-ASPNet-with-Formatting-using-C-and-VBNet.aspx
27 Oct 2014 by abdussalam143
now write SetZeroToNull() method in code behind like. private string SetZeroToNull(string value) { if (value == "0") { return null; } else { return value; } }or you can write...
24 Jan 2013 by abel406
Hi fellows. I have a DataGridView control with a CheckBoxColumn in it, and a CheckBox on the Header. There are some articles regarding this topic , but, i want the DataGridView to behave like i said in the Subject, like yahoo or gmail when you select the checkbox of an email and the...
12 Jun 2012 by Abey Kuruvila
Hi.Got an issue with ScrollBar on my Custom DataGridView.I've inserted the custom DGV on a tabcontrol-tabpage and filled it with rows and performed my custom formatting on it.But when I switch between tabs and return back to the original tab, the scroll bars go...
12 Jun 2012 by Abey Kuruvila
OK...I found out the cause for this.Since I was implementing multiple row clicks (i.e. 3 rows being selected on one row click), I was using the clearselection() method to clear the previous selections.The problem now lies with the clearselection() method that causes the scroll bars to get...
14 Jun 2012 by Abey Kuruvila
Hi!I need help.I want to format part of a String for a DataGridViewCel in vb.net.The thing that I'm doing is, inputting details of a person in a datagridview.So the data is supposed to be formatted with different fonts and then displayed on the DGV cell.SO bascially my data consists...
13 Sep 2012 by Abhai Oza
Just Clear Dataset or Datatabel orBind query to set order by Name. ofr eg:Select MastName,MastId from MastTabel order by MastName.
9 Feb 2015 by Abhai Oza
try thishttp://www.worldbestlearningcenter.com/index_files/csharp-entity-framework-datagridview-add-delete-edit-rows.htm[^]http://stackoverflow.com/questions/15424768/datagridview-cell-edit-and-save-functionality-in-windows-forms[^]
5 Apr 2017 by AbhijitT.Net
My DataSet is returning 5 rows with 5 columns of data. But when i tries to fill the datagridview in loop of DataTable, resultant datagridview shows all table data in single row. Something like cascading effect. Here is my code:foreach (DataTable table in (GetSeatLayout(1).Tables){ ...
8 Jan 2014 by Abhinanda2491
right click option to insert,edit,delete,add new row,add above,add below
8 Jan 2014 by Abhinanda2491
private void cmbInNa_SelectedIndexChanged(object sender, EventArgs e) { try { //DataTable dt = pbl.getdatagridview(this.cmbInNa.Text.ToString()); //if (dt != null && dt.Rows.Count > 0) { ...
10 Jan 2014 by Abhinanda2491
public void fnsave() { int i = 0; foreach (DataGridViewRow row in dataGridView1.SelectedRows) { bool status = false; if (Convert.ToBoolean(String.IsNullOrEmpty(row.Cells["AllowAction"].Value.ToString().Trim()) ?...
17 May 2010 by Abhinav S
Have a look here.Have a look at the last post in the link above for an alternative.
21 Jul 2010 by Abhinav S
Check if any of the values in the cells are null.If they are you need to modify your code to check for null conditions before using ToString().
1 Aug 2010 by Abhinav S
Try this - void myMethod(){ try { } catch (IndexOutOfRangeException Ex) { } catch (Exception Ex) { }}
5 Aug 2010 by Abhinav S
Try DataGridView.FirstDisplayedScrollingRowIndex.
22 Oct 2010 by Abhinav S
See here[^]. Notice how this is done using a bound column in this case.This can also be achieved using an unbound column.
24 Oct 2010 by Abhinav S
Try Convert.ToDecimal(DgvLog.Rows[i].Cells["selfPriceDataGridViewTextBoxColumn"].Value); instead.Convert.ToDecimal will try to parse your string - a cast using decimal (as you have done) is useful when the item being cast is already a number.
13 Nov 2010 by Abhinav S
What error are you getting? For rounding off, have a look here[^].
14 Nov 2010 by Abhinav S
You could start with here[^].As suggested, search on the internet and will find tons of information.
18 Dec 2010 by Abhinav S
I'd add 'Select' to the query (as a UNION) that you use for the datasource.
19 Feb 2011 by Abhinav S
This[^] example should definitely help you out.
7 May 2011 by Abhinav S
Some articles that explain exporting to excel (from a datagridview) - Exporting a DataGridView to Excel in .NET 2.0 (C# code)[^]http://stam.blogs.com/8bits/2010/09/c-datagridview-to-excel.html[^]
26 Jun 2011 by Abhinav S
Try calling the Clear() property on the DataGridView before calling the grid view for second time.
4 Oct 2011 by Abhinav S
See this[^]. It may help you.
1 Nov 2011 by Abhinav S
Have a look at the ColumnHeaderMouseClick[^] and the ColumnHeaderMouseDoubleClick[^] events.
14 Dec 2011 by Abhinav S
DataGridView Filter Popup[^] is a good filter implementation for you to look at.This[^] is a good article on implementing a filter as well.Datagridview with filtering capability[^] is another nice article.You can user DataViewRowState to filter data. Use DataViewRowState to filter...
6 Feb 2012 by Abhinav S
Tryif (this.dataGridView1.DataSource != null){ this.dataGridView1.DataSource = null;}else{ this.dataGridView1.Rows.Clear();}
8 Feb 2012 by Abhinav S
These forum discussions may help you get started - http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/38dc504b-5e5f-43ee-bdc7-045adc335ba5[^]http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/0a63a483-5b15-40d6-afb4-8add6b4f244f[^]
27 Feb 2012 by Abhinav S
Yes it is possible.You can search through the datasource for the grid, find the text that was entered in the textbox, and then highlight the first row where this search is found.Given you have keys that can help you identify rows in your source and grid, it should be fairly easy to do.
2 Apr 2012 by Abhinav S
To customize sort in a datagridview, go through this article - How to: Customize Sorting in the Windows Forms DataGridView Control[^].
5 Sep 2012 by Abhinav S
Try debugging your source code and figure out which line throws the error.
21 Feb 2013 by Abhinav S
'"+Cust_name.Text+"' is a text entry that you are storing into your database.However, the error message suggests your table expects to store an integer type.If your Cust.Text is a valid integer, remove the ' from the query.If it is text, you need to fix your code to pass an integer into...
10 Mar 2013 by Abhinav S
theRow.Cells[colIndex].Value appears to have become null for this particular scenario.When you try to convert null to a string using toString, you will get this error.Check why you are getting null in this value.If null is unavoidable, check for null before converting to string or use...
12 Mar 2013 by Abhinav S
You can get the last row cell value as described here[^].Once you have this value, simply do string.contains[^] on this text.
24 Oct 2013 by Abhinav S
Expose a property (for auto id) in the second form.Access it in the first form and set a value in the second form to true.
27 Jan 2014 by Abhinav S
Try http://techiethings.blogspot.in/2010/05/get-wpf-datagrid-row-and-cell.html[^]http://www.valuedinformation.info/2013/06/how-to-get-datagrid-selected-row-column.html[^]
6 Feb 2014 by Abhinav S
Instead of DataGridView1.Columns(0).DataPropertyName = "FAGRN"you probably need to set the DisplayMember and ValueMember properties since this is a drop down type column.
1 May 2014 by Abhinav S
Assign the datasource of the first datagrid to the second datagrid.This way you will have two grids displaying the same data.
3 May 2014 by Abhinav S
Try the solutions on the following threads -datagridview font change of only one colomn[^]http://social.msdn.microsoft.com/Forums/windows/en-US/8858a7a8-f964-4eac-9733-a874f0f14745/how-to-change-cell-font-in-a-datagridview-control?forum=winformsdatacontrols[^]
21 Mar 2015 by Abhinav S
The easiest solution would be to set the ReadOnly property to true.Another approach could be to make the cell style transparent.Style = { ForeColor = Color.Transparent,SelectionForeColor = Color.Transparent }
16 Apr 2015 by Abhinav S
DataGridView CellValueChanged event[^] is probably what you are looking for.
11 Oct 2015 by Abhinav S
Try these links -Add Image to C# DataGridView [^]Image Thumbnail Preview in DataGridView[^]DataGridViewImageColumn.Image Property[^]
18 Nov 2015 by Abhinav S
Debug and run the query in the backend (sql server studio).You will get a better idea about what could be wrong with the query.
11 Feb 2010 by Abhishek Sur
I think if you create the relationship within the dataset, and put the dataset to the DataGrid's DataSource, it will automatically show you the relationship in the view. DataRelation drelation = new DataRelation("RelationName", ds.Tables[0].Columns["Col1"],...
17 Aug 2014 by aboda yehia
hello guys i am getting this error"Index was out of range. Must be non-negative and less than the size of the collection.Parameter name: index" private void ExcelDGV_CellClick(object sender, DataGridViewCellEventArgs e) { ...
17 Aug 2014 by aboda yehia
Hello GuysI am just wondering if there is a way where i can clear any additional or plus space that i don't need or any symbols like (-,*,/,\,.......Etc).Thanks
24 Apr 2014 by aboubkr90
Since this is your own library I don't know if you did this, but...Usually sender is the validating control and the one you need to call Validated on.
17 Nov 2015 by AcidUser85
I think I found a bug in DataGridView. When I select item in DataGridView Combobox only by mouse, then dirtystate event fires 2 times. First time when drop down is opened, and second time when I select an item from Combobox dropdown. I have overriden ProcessCmdKey so I can now open Combobox...
28 Sep 2014 by aciobanita constantin
Hello,I have a DataGridView and i what to fill with 50000 record but it taken about 45s.Is there a faster way.In the form whare i have the datagridview i use this code to fill it:DataTable dataIN = new DataTable(); public winContracteActive() { ...
7 Apr 2015 by aciobanita constantin
Hello,i have a datagridview, has set a bindingsorce set.works fine wen my datasource has rows, if is emty, i have a emty row im my datagrid but i whant my datagridvie with no row.retrive data from database:internal static ProduseStoc[] ProduseVanzare() { ...
20 Jun 2012 by Adam David Hill
Here's a pseudo-code solution just to give you a simple algorithm, which should put you on the right lines. The actual re-colouring bit I can't remember the code for off the top of my head so take the essence of what it is doing rather than the actual code. If you're still stuck, then please...
2 Aug 2012 by AdamsEthan
HiI am trying to add individual records from a sql server database into my wpf datagrid. The Problem i am having is that each time i want to add a new item to the datagrid, the datagrid deletes the previous item and only the new one is displayed.This is the code for my button that adds...
22 Feb 2021 by adetroxx12
Hey! Basically what I need to do is, I have a lot of data that takes up 1 cell per row loaded into a datagridview, and I want to pivot it, and export it to an excel file. So what I have is a vertical list, and I would like to make it horizontal,...
18 Dec 2020 by Adilcan Topuz
The CellEndEdit event is running when I edit the datagridview cells. but when I print the data from the database to the DataGridView cell, CellEndEdit does not work. dataGridView1.Rows[0].Cells[0].Value = "test"; How can I run CellEndEdit? THANKS... Codes: private void btnKaydet_Click(object...