C++/CLI中DataGridView

在CLI编程中,有个DataGridView表格,可以显示相关的数据。它有三种模式:

1.无约束模式

  没有绑定数据库或其它物件,而是用手写代码给其填充数据的一种模式,一特产用dataGridView->Rows->Add()的形式加入数据。、

2.有约束模式

  用DataGridView的DataSource来绑定数据源,如数据库或文件。用BindingSource控件很方便用。

3.虚拟模式

  用DataGridView连接到内存中独立的数据源(数据缓存)

以下为无约束模式的例子,是手工在一个表格中显示自己定义的数据,里面都有注释,代码大部分是写在窗体Form1的构造函数中。

代码:

#pragma once

namespace SetGridView {

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

/// <summary>
/// Form1 摘要
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
//Form1的构造函数
InitializeComponent();
//记录当前的单元格及其颜色
OldCellBackColor=Color::Empty;
OldCellForeColor=Color::Empty;
highlightedCell=nullptr;

//填充单元格的数据
array<Object^>^ book1={gcnew DateTime(2011,1,8),L"ISBN-01",L"平凡的世界",L"路遥",L"人民邮电出版社"};
array<Object^>^ book2={gcnew DateTime(2003,4,15),L"ISBN-Vs",L"最后的日子",L"Svte Johi",L"阿拉伯出版社"};
array<Object^>^ book3={gcnew DateTime(2008,10,20),L"ISBN-06",L"优美时光",L"孟伟",L"人民大学出版社"};
array<Object^>^ book4={gcnew DateTime(1998,6,7),L"ISBN-78",L"怀念父亲",L"李小珞",L"延边人民出版社"};
array<Object^>^ book5={gcnew DateTime(1990,4,15),L"ISBN-Vt",L"海湾战争",L"Jifsl SVe Micvvf",L"半岛电台博文"};
array<Object^>^ book6={gcnew DateTime(2004,5,18),L"ISBN-89",L"回家",L"在他乡",L"科技出版社"};
array<Object^>^ book7={gcnew DateTime(2006,12,20),L"ISBN-6",L"纵横战场",L"张召忠",L"军事周刊"};

array<array<Object^>^>^ books={book1,book2,book3,book4,book5,book6,book7};
array<String^>^ headers={L"日 期",L"ISBN",L"标 题",L"作 者",L"出 版 社"};
//设置dataGridView表头
dataGridView1->ColumnCount=headers->Length;
for(int i=0;i<headers->Length;i++)
dataGridView1->Columns[i]->Name=headers[i];
for each(array<Object^>^ book in books)
dataGridView1->Rows->Add(book);
//设置dataGridView的一些属性
dataGridView1->Dock=DockStyle::Fill;
dataGridView1->AutoResizeColumns();
dataGridView1->AutoSizeColumnsMode=DataGridViewAutoSizeColumnsMode::AllCells;
dataGridView1->EnableHeadersVisualStyles=true;/*根据应用程序来设置的,如果为false则根据自己的设置来绘制*/

//设置表头的属性
DataGridViewCellStyle^ headerStyle=gcnew DataGridViewCellStyle;
headerStyle->Font=gcnew System::Drawing::Font("Times New Roman",12,FontStyle::Bold);
headerStyle->BackColor=Color::AliceBlue;
headerStyle->ForeColor=Color::BurlyWood;
dataGridView1->ColumnHeadersDefaultCellStyle=headerStyle;

dataGridView1->AutoResizeColumnHeadersHeight();

//表头的提示
for each(DataGridViewColumn^ column in dataGridView1->Columns)
column->ToolTipText=L"Click to\nSort rows";

//用事件委托的方式来注册以下三个事件
dataGridView1->CellFormatting += gcnew System::Windows::Forms::DataGridViewCellFormattingEventHandler(this,&Form1::OnCellFormatting);
dataGridView1->CellMouseEnter += gcnew System::Windows::Forms::DataGridViewCellEventHandler(this,&Form1::OnCellMouseEnter);
dataGridView1->CellMouseLeave += gcnew System::Windows::Forms::DataGridViewCellEventHandler(this,&Form1::OnCellMouseLeave);


//
//TODO: 在此处添加构造函数代码
//
}

protected:
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::DataGridView^ dataGridView1;
protected:

private:
//以下为单元格格式化的函数
void OnCellFormatting(Object^ sender,System::Windows::Forms::DataGridViewCellFormattingEventArgs^ e)
{
if(dataGridView1->Rows[e->RowIndex]->Cells[e->ColumnIndex]==highlightedCell)
return;
if(dataGridView1->Columns[e->ColumnIndex]->Name==L"日 期")
{
if(e->Value!=nullptr&&safe_cast<DateTime^>(e->Value)->Year<2000)
{
e->CellStyle->BackColor=Color::Red;
e->FormattingApplied=false;/*是否对内容格式化*/
}
}
}
//鼠标进入及移出的事件处理
void OnCellMouseEnter(Object^ sender,System::Windows::Forms::DataGridViewCellEventArgs^ e)
{
if(e->ColumnIndex>=0 && e->RowIndex>=0)
{
highlightedCell=dataGridView1->Rows[e->RowIndex]->Cells[e->ColumnIndex];
//保存旧的颜色
OldCellBackColor=highlightedCell->Style->BackColor;
OldCellForeColor=highlightedCell->Style->ForeColor;

//设置当前的颜色
highlightedCell->Style->BackColor=Color::Blue;
highlightedCell->Style->ForeColor=Color::Red;
//DataGridViewCell^ cell=dataGridView1->Rows[e->RowIndex]->Cells[e->ColumnIndex];

}
}

void OnCellMouseLeave(Object^ sender,System::Windows::Forms::DataGridViewCellEventArgs^ e)
{
if(e->ColumnIndex>=0&&e->RowIndex>=0)
{
//DataGridViewCell^ cell=dataGridView1->Rows[e->RowIndex]->Cells[e->ColumnIndex];
highlightedCell->Style->BackColor=OldCellBackColor;
highlightedCell->Style->ForeColor=OldCellForeColor;
OldCellForeColor=OldCellBackColor=Color::Empty;
highlightedCell=nullptr;
}
}

//以下为自己的私有变量
Color OldCellBackColor;
Color OldCellForeColor;
DataGridViewCell^ highlightedCell;
/// <summary>
/// 必需的设计器变量。
/// </summary>
System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
void InitializeComponent(void)
{
this->dataGridView1 = (gcnew System::Windows::Forms::DataGridView());
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->dataGridView1))->BeginInit();
this->SuspendLayout();
//
// dataGridView1
//
this->dataGridView1->ColumnHeadersHeightSizeMode = System::Windows::Forms::DataGridViewColumnHeadersHeightSizeMode::AutoSize;
this->dataGridView1->GridColor = System::Drawing::SystemColors::Control;
this->dataGridView1->Location = System::Drawing::Point(0, 0);
this->dataGridView1->Name = L"dataGridView1";
this->dataGridView1->RowTemplate->Height = 23;
this->dataGridView1->Size = System::Drawing::Size(284, 262);
this->dataGridView1->TabIndex = 0;
this->dataGridView1->CellContentClick += gcnew System::Windows::Forms::DataGridViewCellEventHandler(this, &Form1::dataGridView1_CellContentClick);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(430, 320);
this->Controls->Add(this->dataGridView1);
this->Name = L"Form1";
this->Text = L"Form1";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->dataGridView1))->EndInit();
this->ResumeLayout(false);

}
#pragma endregion
private: System::Void dataGridView1_CellContentClick(System::Object^ sender, System::Windows::Forms::DataGridViewCellEventArgs^ e) {
}
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
}
};
}

显示结果:

  

posted on 2011-10-07 15:07  天上星  阅读(2298)  评论(0编辑  收藏  举报

导航