ARTICLE

A GridView Component in C#

Posted by Mike Gold Articles | Windows Forms C# April 05, 2000
This example shows you how I've wrapped a ListView in a UserControl and turned it into a simple grid control.
Reader Level:
Download Files:
 

Tools Used: Visual C#.NET

You may be asking yourself:  What's a GridView?  I certainly didn't see it in the toolbox. Though it would be nice to have one... This example shows you how I've wrapped a ListView in a UserControl and turned it into a simple grid control. Many times I find myself not wanting to think about items, subitems, listitems, whatever.  I just want to put a string in a row and column position, or populate a row, or populate a column.  I suspect Microsoft never intended the ListView to be used as a Grid, but so many people were doing it, they enabled the capability for using it as a grid without supplying the methods. The GridView class wraps a bunch of the powerful ListView methods and properties, and gives you an easy means of sticking a string in a row and column position.



Fig 1.01

Below is the code for populating the line you see in Fig 1.01:

public Form1()
{

//
Required for Windows Form Designer support
//
InitializeComponent();
// set the column names at the top
gridView1.SetColumnNames(new string[]
{
"FirstName",
"LastName",
"Company",
"E-mail"
}
);
gridView1.SetColumnWidth(0, 10);
// sets the width of the column in font units
gridView1.SetColumnWidth(1, 10); // sets the width of the column in font units
gridView1.SetColumnWidth(2, 15); // sets the width of the column in font units
gridView1.SetColumnWidth(3, 20); // sets the width of the column in font units
//
populate a row of data
gridView1.SetFullRow(2, new string[]{"John", "Doe", "Microsoft", joe@aol.com});
}

You can also use the gridView UserControl1 to
set individual cells:

gridView1.SetCell(4, 3, bob@hotmail.com);


The Design of the Control is fairly easy. It just takes advantage of all the features of a ListView and wraps them so the control behaves as a grid. Below is the code
for populating a cell in a GridView. Note that it works around the problem of a SubItem starting in column 1 (the second column).

public void SetCell(int aRow, int aColumn, string text)
{
if (aRow >= listView1.ListItems.Count) // precondition
{
MessageBox.Show("SetCell:Row out of range");
return;
}
if (aColumn >= listView1.Columns.Count) // precondition
{
MessageBox.Show("SetCell:Column out of range");
return;
}
// populate the cell at the position specified by row and column
if (aColumn == 0)
{
listView1.ListItems[aRow].Text = text;
}
else
{
listView1.ListItems[aRow].SetSubItem(aColumn - 1, text);
}
}

Once the UserControl is installed in your project, you can use it from the toolbox and size the grid to what you need. Also the Grid can be constructed with the dimensions of rows and columns you are using:

this.gridView1 = new GridViewSample.GridView (20,4);

It has really become easy to create your own components. This one could be expanded to have editing capability like Excel.  Hope you find it useful!

Login to add your contents and source code to this article
post comment
     

Good Article

Posted by balaji selvarajan Jul 03, 2012

How do i install this UC in MVS2010?

Posted by Thomas Rossen Mar 08, 2011

Hi all, I have a prob updating the datagrid. i have may forms in my application. first form is login form. in second form i have a datagrid control along with a button add new user. once the new user button is clicked in the second form, a new form will be displayed. this new user form will have test boxes to enter the username and the password along with ok button. once the ok button is clicked, the data entered should be displayed in the datagrid. i have not used any database. i have just used windows forms. pls help me to solve this prob. with regards, Chaitra

Posted by chaitra bharadwaj Jan 15, 2011

And in asp, How do that without datasource?

Posted by Luis Vidal Feb 06, 2007
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts