天天看點

Windows Mobile 5.0 .NET 使用DataGrid

本文講述如何使用DataGrid顯示資料,及如何編輯DataGrid中顯示的資料。

效果圖如下:

Windows Mobile 5.0 .NET 使用DataGrid

首先,示範如何使用DataGrid顯示資料:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace PocketPC

{

public partial class editableGrid : Form

{

//used to fill data into DataGrid

private DataSet ds = new DataSet();

private DataTable dt = new DataTable();

private DataRow dr;

//used to edit cell

private DataGridCell editCell;

private bool inEditMode = false;

private bool inUpdateMode = false;

public editableGrid()

{

InitializeComponent();

fillDataGrid();

}

//init datagrid

private void fillDataGrid()

{

System.DateTime dateTime;

dt.Columns.Add("Name", Type.GetType("System.String"));

dt.Columns.Add("Age", Type.GetType("System.Int32"));

dt.Columns.Add("Birthday", Type.GetType("System.DateTime"));

dateTime = new DateTime(2009, 3, 15) ;

dr = dt.NewRow();

dr["Name"] = "wahaha";

dr["Age"] = 30;

dr["Birthday"] = dateTime;

dt.Rows.Add(dr);

dr = dt.NewRow();

dr["Name"] = "wuhuhu";

dr["Age"] = 25;

dr["Birthday"] = dateTime;

dt.Rows.Add(dr);

ds.Tables.Add(dt);

gdGrid.DataSource = ds.Tables[0];

}

}

}

然後是編輯:

編輯的設想是這樣的,在點選Grid 的某一個Cell時,顯示一個和該cell一樣大小的TextBox,在編輯完成後,将TextBox中的值寫回到cell中。

代碼如下(注:改方法引自微軟MSDN)

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace PocketPC

{

public partial class editableGrid : Form

{

//used to fill data into DataGrid

private DataSet ds = new DataSet();

private DataTable dt = new DataTable();

private DataRow dr;

//used to edit cell

private DataGridCell editCell;

private bool inEditMode = false;

private bool inUpdateMode = false;

public editableGrid()

{

InitializeComponent();

fillDataGrid();

}

//init datagrid

private void fillDataGrid()

{

System.DateTime dateTime;

dt.Columns.Add("Name", Type.GetType("System.String"));

dt.Columns.Add("Age", Type.GetType("System.Int32"));

dt.Columns.Add("Birthday", Type.GetType("System.DateTime"));

dateTime = new DateTime(2009, 3, 15) ;

dr = dt.NewRow();

dr["Name"] = "wahaha";

dr["Age"] = 30;

dr["Birthday"] = dateTime;

dt.Rows.Add(dr);

dr = dt.NewRow();

dr["Name"] = "wuhuhu";

dr["Age"] = 25;

dr["Birthday"] = dateTime;

dt.Rows.Add(dr);

ds.Tables.Add(dt);

gdGrid.DataSource = ds.Tables[0];

}

//event handler for data grid's CurrentCellChanged event

private void gdGrid_CurrentCellChanged(object sender, EventArgs e)

{

if (!inUpdateMode)

{

if (inEditMode && !gdGrid.CurrentCell.Equals(editCell))

{

// Update edited cell

inUpdateMode = true;

gdGrid.Visible = false;

DataGridCell currentCell = gdGrid.CurrentCell;

gdGrid[editCell.RowNumber, editCell.ColumnNumber] = txtEdit.Text;

gdGrid.CurrentCell = currentCell;

gdGrid.Visible = true;

inUpdateMode = false;

txtEdit.Visible = false;

inEditMode = false;

}

// Enter edit mode

editCell = gdGrid.CurrentCell;

if (editCell.GetType() == Type.GetType("System.String"))

{

txtEdit.Text = (string)gdGrid[editCell.RowNumber, editCell.ColumnNumber];

}

else

{

txtEdit.Text = Convert.ToString(gdGrid[editCell.RowNumber, editCell.ColumnNumber]);

}

Rectangle cellPos = gdGrid.GetCellBounds(editCell.RowNumber,

editCell.ColumnNumber);

txtEdit.Left = cellPos.Left - 1;

txtEdit.Top = cellPos.Top + gdGrid.Top - 1;

txtEdit.Width = cellPos.Width + 2;

txtEdit.Height = cellPos.Height + 2;

txtEdit.Visible = true;

inEditMode = true;

}

}

private void btnGetData_Click(object sender, EventArgs e)

{

DataTable dss = (DataTable)gdGrid.DataSource;

DataRowCollection drc = dss.Rows;

string ret = "";

foreach (DataRow drtmp in drc)

{

ret += Convert.ToString(drtmp["Name"]) + ","+ Convert.ToString(drtmp["Age"]) + "," + Convert.ToString(drtmp["Birthday"]) + '/n';

}

MessageBox.Show(ret);

}

private void txtEdit_LostFocus(object sender, EventArgs e)

{

if (!inUpdateMode)

{

if (inEditMode && !gdGrid.CurrentCell.Equals(editCell))

{

// Update edited cell

inUpdateMode = true;

gdGrid.Visible = false;

DataGridCell currentCell = gdGrid.CurrentCell;

gdGrid[editCell.RowNumber, editCell.ColumnNumber] = txtEdit.Text;

gdGrid.CurrentCell = currentCell;

gdGrid.Visible = true;

inUpdateMode = false;

txtEdit.Visible = false;

inEditMode = false;

}

}

}

}

}

但這中方法有一個弊端,由于在5.0中沒有辦法獲得DataGrid滾動條被點選或拖到的事件,是以如果你在編輯某一個cell時,直接去點滾動條改變DataGrid顯示的記錄,你會發現那個textBox漂在那裡....

是以建議隻在不出現滾動條是使用這樣的方法。

繼續閱讀