Monday, 17 December 2012 13:54

Simple Gridview control example

Written by 
Rate this item
(0 votes)

following codes shows how to use GridView control.Its a
simple example.

in app main file -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace SimpleGridView
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new mridulForm());
}
}
}

place a gridview control(dataGridView1) in form first.In form code behind file,
the codes are as follows.Also place two button and its click events shown in code below -

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SimpleGridView
{
public partial class mridulForm : Form
{
enum productType { MilkTea, LemonTea, Coffe }

public mridulForm()
{
InitializeComponent();
}

private void saveButton_Click(object sender, EventArgs e)
{
int price = 0;
productType Obj = (productType)Enum.Parse(typeof(productType),
itemComboBox.Text.ToString());

if (productType.MilkTea == Obj)
{ price = 5; }
else if (productType.LemonTea == Obj)
{ price = 3; }
else if (productType.Coffe == Obj)
{ price = 8; }
dataGridView1.Rows[0].Cells[0].Value = itemComboBox.Text;
dataGridView1.Rows[0].Cells[1].Value =
price * Convert.ToInt16(itemPriceTextBox.Text);
}

private void button1_Click(object sender, EventArgs e)
{
this.Close();
}

} }

Read 1753 times
Super User

Email This email address is being protected from spambots. You need JavaScript enabled to view it.
Login to post comments