Whenever create a windows application on that case we always face some situation where we needed to display record in grid form and wanted to highlight a single row . So in this application I will show you how you can bind a datagridview control in your windows application using C#. In this I will use ms access database to access the data and highlight a single row.
In my previous article I have shown your how you can bind a gridview control in asp.net application check the article Binding Gridview By Access DataBase Using C#.Net in Asp.Net, Bind
DataGridView In Windows Application Using C#
Some of my previous windows and C# .net application are as follows: How to Minimize an Application to the Taskbar Tray in C#.Net | System Tray Application C#, Email Validation in Windows Application C#.Net and VB.Net | Validating Email ID in TextBox in C# .Net, Displaying More than One Month in the Windows Forms MonthCalendar Control Using C#.Net.
My listbx related articles are as follows: Remove Selected Item From Listbox C#.Net and VB.net, How to add/move multiselected items from one listbox to another listbox in C#.Net and VB.Net | C# multiple selection listbox move Using C#.Net, Transfer Listbox Items to Another Listbox Using C#.Net and VB.Net | How to Move List Box Items to another List Box in C#.
Now for this article first we will create a new windowsapplication and add a DataGridView Control in it.
Now check the below code
C#.Net
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Windows.Forms;
namespace DemoWindowsApplication
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
public string connectionstring ="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=bookstore.mdb;Persist Security Info=False;";
/// <summary>
/// Default constructor for main form
/// </summary>
public MainForm()
{
InitializeComponent();
}
void MainFormLoad(object sender, EventArgs e)
{
DataTable _objdt = new DataTable();
_objdt = GetDataFromDataBase();
if (_objdt.Rows.Count > 0)
{
dataGridView1.DataSource = _objdt;
//Code to hilight
the row
foreach
(DataGridViewRow row in dataGridView1.Rows)
if
(Convert.ToInt32(row.Cells[2].Value) ==
1903)
{
row.DefaultCellStyle.Font = new Font("Arial",
10, FontStyle.Bold);
}
}
}
/// <summary>
/// Function for binding retriving the data from database
/// </summary>
/// <returns></returns>
public DataTable GetDataFromDataBase()
{
DataTable _objdt = new DataTable();
string querystring = "select author_name,title,publication_year from Books;";
OleDbConnection _objcon=new OleDbConnection(connectionstring);
OleDbDataAdapter _objda = new OleDbDataAdapter(querystring, _objcon);
_objcon.Open();
_objda.Fill(_objdt);
return _objdt;
}
}
}
|
VB.Net
Imports System
Imports System.Collections.Generic
Imports System.Data
Imports System.Data.OleDb
Imports System.Drawing
Imports System.Windows.Forms
Namespace DemoWindowsApplication
''' <summary>
''' Description of MainForm.
''' </summary>
Partial Public Class MainForm
Inherits Form
Public connectionstring As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=bookstore.mdb;Persist Security Info=False;"
''' <summary>
''' Default constructor for main form
''' </summary>
Public Sub New()
InitializeComponent()
End Sub
Private Sub MainFormLoad(ByVal sender As Object, ByVal e As EventArgs)
Dim _objdt As New DataTable()
_objdt = GetDataFromDataBase()
If _objdt.Rows.Count > 0 Then
dataGridView1.DataSource = _objdt
'Code to
hilight the row
For Each row As
DataGridViewRow In dataGridView1.Rows
If
Convert.ToInt32(row.Cells(2).Value) = 1903 Then
row.DefaultCellStyle.Font
= New Font("Arial", 10,
FontStyle.Bold)
End
If
Next
End If
End Sub
''' <summary>
''' Function for binding retriving the data from database
''' </summary>
''' <returns></returns>
Public Function GetDataFromDataBase() As DataTable
Dim _objdt As New DataTable()
Dim querystring As String = "select author_name,title,publication_year from Books;"
Dim _objcon As New OleDbConnection(connectionstring)
Dim _objda As New OleDbDataAdapter(querystring, _objcon)
_objcon.Open()
_objda.Fill(_objdt)
Return _objdt
End Function
End Class
End Namespace
|
In above code below mention code is used for highlighting the row.
//Code to hilight the row
foreach (DataGridViewRow row in dataGridView1.Rows)
if (Convert.ToInt32(row.Cells[2].Value)
== 1903)
{
row.DefaultCellStyle.Font
= new Font("Arial", 10, FontStyle.Bold);
}
|
Now in above code first we have created a function whose return type is DataTale and on page load we have bind the datagridview control.
Now run the application
0 comments:
Please let me know your view