connectseekers.com

Thursday, 12 June 2014

How To Make a Single Row of DataGridview Bold Using C#.Net in Windows Application

6/12/2014 - By Pranav Singh 0

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.NetBind DataGridView In Windows Application Using 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 ObjectByVal 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



About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top