Some Impotent Data Grid Technique

Submitted by: 
Visitors have accessed this post 2607 times.

Data grid is most powerful component of the .net environment and can be used to populate data web/ and widows averment. In this tutorial I am going to teach you few data handling techniques of data grid control. It gives you to develop data driven application in .net environment and provide some great and needed futures like pagination, sorting, row editing etc.

I am here going to introduce some of the coding technique that is very useful to development of data grid base applications.

At first of all we need to know how to bind the data. Data grid automatically binds the data sources like dataset/ IList, IBindingList, IListSource etc. and capable of automatically load the data.

  1. List<Customer> customerList = new List<Customer>();
  2. grdCustomer.DataSource = customerList;

Above code automatically load the data without any formatting. Some time you need to show formatted data on data grid situation like table has large column and we want to visualized some.

  1. void CreatingGrid(List<Company> CmpnyList)
  2. {
  3. DataTable dtCompany = new DataTable();
  4. dtgCompany.DataSource = dtCompany;
  6. dtCompany.Columns.Add("COMPANY ID");
  7. dtCompany.Columns.Add("COMPANY CODE");
  8. dtCompany.Columns.Add("COMPANY NAME");
  9. dtCompany.Columns.Add("COMPANY ADDRESS");
  10. dtCompany.Columns.Add("COMPANY PHONE");
  11. //dtCompany.Columns.Add("Edit");
  14. foreach (Company company in CmpnyList)
  15. {
  17. dtCompany = (DataTable)dtgCompany.DataSource;
  19. dtCompany.Rows.Add(dtCompany.NewRow());
  23. dtCompany.Rows[dtCompany.Rows.Count - 1][0] = company._com_id.ToString();
  24. dtCompany.Rows[dtCompany.Rows.Count - 1][1] = company.com_code.ToString();
  25. dtCompany.Rows[dtCompany.Rows.Count - 1][2] = company.com_name.ToString();
  26. dtCompany.Rows[dtCompany.Rows.Count - 1][3] = company.com_address.ToString();
  27. dtCompany.Rows[dtCompany.Rows.Count - 1][4] = company.com_PhoneNo.ToString();
  30. }
  33. }

Then I am going to show you how to add a row to our customer data grid.

  1. grdCustomer. .Rows.Add() ;
  2. int rowIndex = grdCustomer.RowCount -1;
  3. DataGridViewRow newCustomerRow = grdCustomer.RowIndex[rowIndex];
  4. newCustomerRow .Cell[“NAME”] = “JOHN”;
  5. newCustomerRow .Cell[“ADDRESS”] = “COLOMBO”;
  6. newCustomerRow .Cell[“AGE”] = 30;
  7. newCustomerRow .Cell[“EMAIL”] =<a href="mailto:[email protected]" rel="nofollow">[email protected].COM</a>;

Selecting a data row of data grid is another very useful functionality.

  1. private void dtgCompany_CellContentClick(object sender, DataGridViewCellEventArgs e)
  2. {
  4. if (e.ColumnIndex == 0)
  5. {
  8. int rowNumber = e.RowIndex;
  10. if (!DBNull.Value.Equals(dtgCompany["COMPANY ID", rowNumber].Value))
  11. {
  12. int grdRowRow = Convert.ToInt32(dtgCompany["COMPANY ID", rowNumber].Value);
  13. Company company_O = CompanyDAO.GetCompanyById(grdRowRow);
  14. this.bingObjectToControls(company_O);
  16. Locationfrm loc_frm = new Locationfrm();
  17. loc_frm.Show();
  18. }
  19. }
  21. }

Deleting data row on grid without any errors is very important. To avoid occurring errors we have to synchronize data grid with data sources. you must find the proper row in the Data Source using the primary key of the selected row. You can retrieve the selected row's primary key by calling

string key = grdCusomer.DataKeys[e.Item.ItemIndex].ToString();

This is works when you have assign primary key field on data grid. Then you can find the corresponding row on data grid using above key and delet the row from datagrid.

  1. DataRow selectedRow = grdCusomer.FindByau_id(key);
  2. selectedRow.Delete();

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.