Set SelectedIndex for DataGridViewComboBoxCell

Đoạn code sau hướng dẫn cách thiết lập DataGridViewComboBox Column trong windows Forms

DataTable dt = null;
private void Form1_Load(object sender, EventArgs e)
{
dt = new DataTable();
dt.Columns.Add(“Category”);
DataRow dr = dt.NewRow();
dr[“Category”] = “Select All”;
dt.Rows.Add(dr);
DataRow row = dt.NewRow();
row[0] = “Cat 1”;
dt.Rows.Add(row);
DataRow row2 = dt.NewRow();
row2[0] = “Cat 2”;
dt.Rows.Add(row2);
DataGridViewComboBoxColumn newColumn = new DataGridViewComboBoxColumn();
newColumn.Name = “Category”;
newColumn.DataSource = dt;
newColumn.DisplayMember = “Category”;
newColumn.ValueMember = “Category”;
dataGridView1.Columns.Add(newColumn);
}

private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
this.dataGridView1.Rows[e.RowIndex].Cells[0].Value = dt.Rows[0][0].ToString();
}

Script check database and table was existed

Script check database and table was existed

DECLARE @dbname nvarchar(128)
SET @dbname = N’dbTest5′

IF (EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE (‘[‘ + name + ‘]’ = @dbname OR name = @dbname)))
begin
use master
drop database dbtest5
end
go
create database dbtest5
go

use dbtest5
go

— Run this script at any time, either to:
— (a) Create the demo tables in a different database (see note in previous example)
— (b) Restore the demo tables to their original state

if exists (select * from sysobjects where name = ‘PurchaseItem’) drop table PurchaseItem
if exists (select * from sysobjects where name = ‘Purchase’) drop table Purchase
if exists (select * from sysobjects where name = ‘Customer’) drop table Customer
if exists (select * from sysobjects where name = ‘MedicalArticles’) drop table MedicalArticles
if exists (select * from sysobjects where name = ‘Product’) drop table Product
go

create table Customer
(
ID int not null primary key,
Name nvarchar(30) not null
)

create table Purchase
(
ID int not null primary key,
CustomerID int null references Customer (ID),
Date datetime not null,
Description varchar(30) not null,
Price decimal not null
)

create table PurchaseItem
(
ID int not null primary key,
PurchaseID int not null references Purchase (ID),
Detail varchar(30) not null,
Price decimal not null
)

create table MedicalArticles
(
ID int not null primary key,
Topic varchar (20),
Abstract nvarchar (2000)
)

create table Product
(
ID int not null primary key,
Description varchar(30) not null,
Discontinued bit not null,
LastSale datetime not null
)

Table per hierarchy – type 1

public class Course
{
[Key]
public int CourseId { get; set; }
public string Name { get; set; }
}
public class OnlineCourse : Course
{
public string URL { get; set; }
}
public class OfflineCourse : Course
{
public string Address { get; set; }
}

public class TPHContext:DbContext
{
public DbSet<Course> Courses { get; set; }
public TPHContext() : base(“TPH”)
{
Database.SetInitializer<TPHContext>(new DropCreateDatabaseIfModelChanges<TPHContext>());
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Course>()
.Map<OnlineCourse>(m => m.Requires(“Type”).HasValue(“OnlineCourse”))
.Map<OfflineCourse>(m => m.Requires(“Type”).HasValue(“OfflineCourse”));
}
}