Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to Customize GridView Cells in .NET 2.0 using C#

Author: DevAsp Team
Download Source Code : 665_GridView_ColumnsAccess.zip

In this simple article you will learn how Access DataGridView Cells

Simply just create New Project of Windows application in C#.

 

Drag one Data Grid View on the form and create a database in access with same database which is in Source folder.

 

In this sample I am using MS Access database and getting values from access on run time. This is a good example for beginners.

 

 

Now declare global variables in for

 

System.Data.OleDb.OleDbConnection cn = new System.Data.OleDb.OleDbConnection();

        System.Data.OleDb.OleDbCommand cm = new System.Data.OleDb.OleDbCommand();

        System.Data.OleDb.OleDbDataReader  rd;

        System.Data.OleDb.OleDbDataAdapter  adp;

 

 

 

Now just write code on Form Load Event

 

private void Form1_Load(object sender, EventArgs e)

        {

            this.Text = "Column Access by Clicking on columns";

 

            cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=test.mdb";

            try

            {

                cn.Open();

            }

            catch (Exception Ex)

            {

                MessageBox.Show(Ex.Message);

            }

            cm.Connection = cn;

            cm.CommandText = "select * from tbl1";

            try

            {

                adp = new System.Data.OleDb.OleDbDataAdapter();

                DataSet ds = new DataSet();

                adp.SelectCommand = cm;

                adp.Fill(ds, "dataset");

                dataGridView1.DataSource = ds;

                dataGridView1.DataSource = ds.Tables[0];

                dataGridView1.Refresh();

            }

            catch (Exception Ex)

            {

 

                MessageBox.Show(Ex.Message);

            }

            finally

            {

                cn.Close();

            }

        }

 

This code Load data in DataGrid View.

Data Adaptor fill command fill the Dataset ds. Then we assign Data grid view that Data set and we are able to see database values on grid.

 

 

Now Write code on CellClick Event of dataGrid To Access Data,data Type,CellNo in GridView.

 

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)

        {

            if (e.RowIndex > -1 && e.ColumnIndex > -1)

                MessageBox.Show("Column No=" + e.ColumnIndex.ToString() + " Row No=" + e.RowIndex.ToString() + " Value =" + dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() + " Value Type=" + dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].ValueType.ToString());

            

            else

                MessageBox.Show("Column No=" + e.ColumnIndex.ToString() + " Row No=" + e.RowIndex.ToString());

        }

 

Now Message Box Show you All information about the Cell of grid on which you Clicked.

 

Article Comments
Hi,
I'm creating a datagridview at run time itself.
I tried to use the click event for that run time control as u have mentioned above... but it failed to work. can u give me a solution.

I'm using the same datagridview to be shown in different tabs that i create at runtime. So when i select one tab and choose a datagrid cell the event has to get triggered by giving me the entire data in that row in a for loop....
Please advice.
Regards,
Mahesh

Posted on 6/20/2007 12:37:29 AM by mahesh

Hello,
How are you,i hope you are fine.
I have one problem about DataGridView
How to change in run time column index
I am execute the query in dataset so result is right and but execute the program so column index is differant display in dataGridView.
Please tall me sir.
Thanks
Brajesh Kumar

Posted on 7/9/2007 8:35:02 AM by Brajesh Kumar Ahirwar

i have datagrid in asp.net framework2.0 ,I have select buttouns in the first column and i need to fetch the first column value which is visible false in rowcommand or selected index change event but i get an error saying that the index out of range and it must be a non negative and less than the size of collection

Posted on 7/19/2007 11:25:36 AM by shubhashree

hi
i am trying this code for displaying data in datagridview but it displays only blank cells rather then cells with data of database. i am using MSaccess .cuold u suggest me but should be the problem.

// TODO: This line of code loads data into the 'alumniDataSet1.alumn1' table. You can move, or remove it, as needed.
this.alumn1TableAdapter1.Fill(this.alumniDataSet1.alumn1);
//OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=f:\alumni dir1\alumni.mdb");
string ConString = ConfigurationSettings.AppSettings["ConnectionString"];
OleDbConnection conn = new OleDbConnection(ConString);
conn.Open();
OleDbCommand comm = new OleDbCommand();
comm.CommandText="select * from alumn1";
comm.Connection = conn;
OleDbDataAdapter ad = new OleDbDataAdapter();
ad.SelectCommand = comm;
DataSet ds = new DataSet();

ad.Fill(ds,"alumn1");

dataGridView1.DataSource = ds;

//dataGridView1.DataMember = "alumn1";
dataGridView1.DataSource = ds.Tables[0];


//dataGridView1.DataMember = "alumn1";

conn.Close();

Posted on 10/3/2007 7:54:27 AM by sardar singh

It is very good. It really help me...
Thank you....
Hirak

Posted on 10/26/2007 8:19:13 AM by Hirakmani Das

Hi...i want to customize the gridView upon binding it with the Dataset.e.g there is one column 'Amount Pending' i want to show it as 'Amount Pending(in Rs.)' also i want to set its horizontal alignment to right...how should i do it??? is anyone have a solution for this???

Posted on 12/7/2007 7:05:15 AM by Swati

It was So Nice for Begners.I like it very much.

Posted on 1/28/2008 9:23:59 AM by Waqar Hussain

i am a beginner for Dot.net.I am trying to display the data from the dataSource in the grid view the data should be editable,update and delete.
please can you help me out

Posted on 3/17/2008 6:07:18 AM by Latha

help me i wanna delete the data from gridview at runtime..??not by drag and drop controls ...via coding

Posted on 5/17/2008 6:03:31 AM by sagar

i didnt get the solution can u help me

Posted on 5/30/2008 2:22:45 AM by suba ganesh

Your site in informative site,
but my problem is that
i am displayng records in gridview and want to triger an event onClick on the particular record and show more information in another form accroding to entire particular record.

Posted on 7/8/2008 10:42:53 AM by sohail Basheer

it is best for me

Posted on 9/24/2008 2:57:04 PM by chandan kumar saw

hi all

i create displaying image in gridview with pagination... but images are displayed only in vertical view i want horizontal view images how to create in asp.net using c# and also using layers?

Posted on 12/18/2008 1:42:43 AM by priya

Hi, i want to create a gridview with edit, update and deleteoptions in windows application using c# and database is sql server 2005. can any one please help me in this??

Posted on 1/19/2010 8:19:57 AM by rajesh

hi..
i want to create a datagridview with edit and update in windows application using c# and database is ms-access 2003. i don have much idea about this. So can any one please help me to do this??

Posted on 3/27/2010 9:42:26 AM by Nageswari

hi...this code very good...but i want to edit database in datagridview using oledb connection please give code for that on my e-mail address.thanks

Posted on 4/6/2010 10:11:29 AM by shingala

Hi
this code very very good.I want to select a cell from grid view.if anyone know the code for my prob.send to my email id.

Posted on 4/20/2010 5:39:36 AM by Ravi Kumar

hi,
windows application in c sharp examples using gridview how to add check box in item template

example

gridview row count 4
i need check box row also 4

Posted on 5/17/2010 5:13:15 AM by Murugan

hi,
i created datagrid view & insert values at runtime.
i want restrict some cells tabstop plz give me the solution of this problem.

Posted on 6/4/2010 3:36:11 AM by sandesh

hi...
all developers,programers,,,

i want send me a sample grid view source code ...i created html code 4 columns and their bound fields ..but how to write a source code plz send me

Posted on 7/23/2010 8:09:15 AM by rajendra

Hi, i want to create a gridview with edit, update and deleteoptions in windows application using c# and database is sql server 2005. and how i need to ensure all fields has value before storing in the database can any one please help me in this??

Posted on 10/6/2010 8:15:04 AM by shihabudheen v.p

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to Send/Receive Command Line Parameter .NET 2.0 using C#

Disclaimer - Privacy
© 2002-2017 DevASP.net