全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:13056
推到 Plurk!
推到 Facebook!

VS2003 C# 使用的 TStringGrid 元件 (VCL LIKE)

 
digitraveler
初階會員


發表:89
回覆:91
積分:46
註冊:2005-06-01

發送簡訊給我
#1 引用回覆 回覆 發表時間:2009-02-11 00:05:47 IP:218.173.xxx.xxx 訂閱
■ VS2003 C# 使用的 TStringGrid 元件 (VCL LIKE)
完整文章請見 C# 源碼任務 http://tw.myblog.yahoo.com/bruce0211/

VS2003 C# 沒有 TStringGrid
沒用過 Borland VCL TStringGrid 元件的人不知道它的好用
對每次都要綁定 DataTable DataGrid 兩個元件來操作相同功能甘之如飴
但小弟可不這麼想 , 一直覺的 .NET 元件很笨很難用
想自製一個 TStringGrid (基本上就是 DataTable DataGrid 兩個元件的組合)
來處理單純的二維 String 資料顯示
目前只實做了最常用又熟悉的三個 VCL 屬性
1.StringGrid.RowCount 指定或取得 列數
2.StringGrid.ColCount 指定或取得 行數
3.StringGrid.Cells[y][x] 指定或取得 Cell[y][x] 內容值
在此拋磚引玉示範了 C# 元件的開發範例 , 其它 VCL LIKE 屬性由您慢慢擴增
本元件有幾個特色及建議
1.拿掉 DataGrid 討厭的空白值的 "(null)" 提示 (真懷疑該 C# 元件開發者有沒審美觀念)
2.屬性名稱盡量跟我們熟悉的 Borland VCL 屬性相同 (所以叫 VCL LIKE)
3.希望集合大家的力量來開發一些 VCL LIKE 元件 , 不用重寫新元件 , 拿現成類似的 C# 元件來包裝一下屬性即可 , 使用上就可以用熟悉的語法來控制元件外觀跟操作 , 最重要的是不用 Borland 工具就可使用接近 VCL 風格的元件
4.使用 VS2003 是因小弟學 C# 是自修玩票性質
//TStringGrid.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace TStringGrid
{

public class TStringGrid : System.Windows.Forms.DataGrid
{
private System.ComponentModel.Container components = null;
private System.Data.DataTable mDataTable;
private int iRowCount=0;
private int iColCount=0;


public TStringGrid()
{
InitializeComponent();

mDataTable = new DataTable("ParentTable");
this.DataSource = mDataTable;

SetColCount(5);
SetRowCount(5);


}

private void InitializeComponent()
{
this.Name = "StringGrid";
this.Size = new System.Drawing.Size(296, 224);

//this.CaptionBackColor=SystemColors.Control;
//this.BackgroundColor=SystemColors.Window;
//設定 dataGrid1 基本屬性
this.CaptionVisible=false; //不要秀 caption
this.RowHeadersVisible=false; //不要秀 FixedCol;
this.AllowSorting=false; //不要排序鈕
//this.ReadOnly=true; //尾巴不要有新的空白列
//this.Enabled=false; //只能由程式編輯資料
this.Font = new Font("Arial", 10); //設定字型
}
protected override void Dispose( bool disposing )
{
if ( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}


private void SetRowCount(int irow)
{
if (irow==iRowCount) return;
DataRow dr;
if (irow > iRowCount)
{
for (int i=iRowCount 1; i<=irow; i )
{
dr = mDataTable.NewRow();
mDataTable.Rows.Add(dr);

//not display "(null)"
for (int x=0; x<=mDataTable.Columns.Count-1; x )
{
mDataTable.Rows[i-1][x]="";
}

}

}
else
{
for (int i=iRowCount; i>=irow 1; i--)
{
mDataTable.Rows.RemoveAt(i-1);
}

}
iRowCount=irow;
}
private void SetColCount(int icol)
{
if (icol==iColCount) return;
if (icol > iColCount)
{
for (int i=iColCount 1; i<=icol; i )
{
mDataTable.Columns.Add("",System.Type.GetType("System.String"));

//not display "(null)"
for (int y=0; y<=mDataTable.Rows.Count-1; y )
{
mDataTable.Rows[y][i-1]="";
}

}
}
else
{
for (int i=iColCount; i>=icol 1; i--)
{
mDataTable.Columns.RemoveAt(i-1);
}
}
iColCount=icol;

}

public int RowCount
{
get{ return this.mDataTable.Rows.Count; }
set
{
SetRowCount(value);
this.Invalidate();
}
}
public int ColCount
{
get{ return this.mDataTable.Columns.Count; }
set
{
SetColCount(value);
this.Invalidate();
}
}
public DataRowCollection Cells
{
get{ return mDataTable.Rows;}
set
{
object r= (object)Cells;
r=value;
}

}




}

}

參考文章
1.Memory Table 的使用
http://tw.myblog.yahoo.com/bruce0211/article?mid=117&prev=119&next=115
2.A simple home-made StringGrid
http://bytes.com/groups/net-c/251986-simple-home-made-stringgrid

編輯記錄
digitraveler 重新編輯於 2009-02-11 00:06:17, 註解 無‧
digitraveler 重新編輯於 2009-02-11 00:21:28, 註解 無‧
digitraveler 重新編輯於 2009-02-11 07:25:51, 註解 無‧
digitraveler
初階會員


發表:89
回覆:91
積分:46
註冊:2005-06-01

發送簡訊給我
#2 引用回覆 回覆 發表時間:2009-02-26 13:29:06 IP:211.21.xxx.xxx 訂閱
替這 VCL LIKE 的 TStringGrid 新增一個方法,兩個屬性

tStringGrid1.SetColCaption("test1,test2,test3"); //設定 Column 的第一列名稱
tStringGrid1.DefaultColWidth=100; //設定 Cell 欄寬
tStringGrid1.DefaultRowHeight=100; //設定 Cell 欄高



//例 : tStringGrid1.SetColCaption("test1,test2,test3");
public void SetColCaption(string ColCaptions)
{
string[] tmp = ColCaptions.Split(',');
int i=0;
foreach (string seg in tmp)
{
if (i<=mDataTable.Columns.Count-1)
mDataTable.Columns[i].ColumnName=tmp[i];

i ;
}
}
public int DefaultColWidth
{
get{ return this.PreferredColumnWidth;}
set
{
this.PreferredColumnWidth=value;

//用此方式來 repaint ??
this.ColCount=this.ColCount 1;
this.ColCount=this.ColCount-1;



}
}

public int DefaultRowHeight
{
get{ return this.PreferredRowHeight;}
set
{
this.PreferredRowHeight=value;

//用此方式來 repaint ??
this.RowCount=this.RowCount 1;
this.RowCount=this.RowCount-1;
}
}
digitraveler
初階會員


發表:89
回覆:91
積分:46
註冊:2005-06-01

發送簡訊給我
#3 引用回覆 回覆 發表時間:2009-02-28 00:31:25 IP:218.173.xxx.xxx 訂閱
VS2003 C# 使用的 TStringGrid 元件 (加強版)
原文網址 http://tw.myblog.yahoo.com/bruce0211/article?mid=165

增加了實用上常用到的欄位的外觀屬性設定 , 包含個別的欄位名稱 , 個別的欄寬 , 個別的靠邊方向 ; 以下控件屬性 , 除了 設定欄位(Cell)資料內容外 , 其餘 VCL_LIKE 屬性都可在開發模式 (Design-Mode) 設定及看到控件外觀立即的變化
GrandRURU
站務副站長


發表:240
回覆:1680
積分:1874
註冊:2005-06-21

發送簡訊給我
#4 引用回覆 回覆 發表時間:2010-12-16 22:03:12 IP:111.249.xxx.xxx 未訂閱
最近才開始學c#,看到了這個元件,感覺相當有趣呢!
謝謝分享!(現在才回會不會太慢?哈哈!)
DavidLo
高階會員


發表:17
回覆:225
積分:168
註冊:2004-07-21

發送簡訊給我
#5 引用回覆 回覆 發表時間:2011-10-24 15:36:21 IP:122.116.xxx.xxx 訂閱
請問Blog中的下載連結,現在都無法使用嗎?
有可能再提供嗎?
謝謝!
系統時間:2024-04-20 11:50:30
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!