在wince和windows moble 开发中发现datagrid使用很不方便,项目中用到了第三方开源控件NTable,却发现这个控件是2004年国外开发的,使用资料及其稀少
现在共享下其开发方法:
下载源码后只需要在自己的数据源里面实现其
: INTableModel接口里面的方法即可
View Code
class TestModel:INTableModel
{
#region ITableModel Members
public int GetRowCount()
{
return 10000;
}
public int GetColumnCount()
{
return 4;
}
public string GetColumnName(int columnIndex)
{
if (columnIndex == 0)
return "序号";
if (columnIndex == 1)
return "供应商";
if (columnIndex == 2)
return "合同号";
if (columnIndex == 3)
return "单号";
return "Description";
}
public Type GetColumnClass(int columnIndex)
{
if (columnIndex == 0)
return typeof(int);
return typeof (String);
}
public bool IsCellEditable(int rowIndex, int columnIndex)
{
return columnIndex == 1;
}
public object GetValueAt(int rowIndex, int columnIndex)
{
if (columnIndex == 0)
return rowIndex+1;
if (columnIndex == 1)
return "供应商";
if (columnIndex == 2)
return "合同号";
if (columnIndex == 3)
return "单号";
return "Tap for enter description";
}
public void SetValueAt(object aValue, int rowIndex, int columnIndex)
{
}
public object GetObjectAt(int rowIndex, int columnIndex)
{
return null;
}
public event TableModelChangeHandler Change;
#endregion
}
{
#region ITableModel Members
public int GetRowCount()
{
return 10000;
}
public int GetColumnCount()
{
return 4;
}
public string GetColumnName(int columnIndex)
{
if (columnIndex == 0)
return "序号";
if (columnIndex == 1)
return "供应商";
if (columnIndex == 2)
return "合同号";
if (columnIndex == 3)
return "单号";
return "Description";
}
public Type GetColumnClass(int columnIndex)
{
if (columnIndex == 0)
return typeof(int);
return typeof (String);
}
public bool IsCellEditable(int rowIndex, int columnIndex)
{
return columnIndex == 1;
}
public object GetValueAt(int rowIndex, int columnIndex)
{
if (columnIndex == 0)
return rowIndex+1;
if (columnIndex == 1)
return "供应商";
if (columnIndex == 2)
return "合同号";
if (columnIndex == 3)
return "单号";
return "Tap for enter description";
}
public void SetValueAt(object aValue, int rowIndex, int columnIndex)
{
}
public object GetObjectAt(int rowIndex, int columnIndex)
{
return null;
}
public event TableModelChangeHandler Change;
#endregion
}
GetColumnName方法可以返回每列的列名
GetValueAt可以把每个格子的数据返回
你可以把自己的数据源的数据绑定上去
下面是过滤器的实现
View Code
public class RequestListDataSource : INTableModel
{
#region ITableModel Members
private EntityType _showType;
private Collection<RequestListRequestListItem> _rows;
private List<RequestListRequestListItem> _filterRows;
public RequestListDataSource(RequestList data, EntityType type)
{
_showType = type;
_rows = new Collection<RequestListRequestListItem>();
_filterRows = new List<RequestListRequestListItem>();
if (data != null)
{
foreach (var item in data.RequestListItem)
_rows.Add(item);
}
_filterRows.AddRange(_rows);
}
public int GetRowCount()
{
//RequestList aa;aa.RequestListItem [0].
return _filterRows.Count;
}
public int GetColumnCount()
{
if (_showType == EntityType.MaterialCKOutSelectListNum)
return 4;
else
return 3;
}
public string GetColumnName(int columnIndex)
{
string result = "";
if (columnIndex == 0)
result = "序号";
if (_showType == EntityType.MaterialCKOutSelectListNum)
{
if (columnIndex == 1)
result = "供应商";
else if (columnIndex == 2)
result = "合同号";
else if (columnIndex == 3)
result = "单号";
}
else
{
if (columnIndex == 1)
result = "领用单位";
else if (columnIndex == 2)
result = "单号";
}
return result;
}
public Type GetColumnClass(int columnIndex)
{
if (columnIndex == 0)
return typeof(int);
else
return typeof(String);
}
public bool IsCellEditable(int rowIndex, int columnIndex)
{
return false;
}
public object GetValueAt(int rowIndex, int columnIndex)
{
object result = null;
if (rowIndex >= _filterRows.Count)
return result;
if (columnIndex == 0)
result = rowIndex + 1;
if (_showType == EntityType.MaterialCKOutSelectListNum)
{
if (columnIndex == 1)
result = GetValueFromType(_filterRows[rowIndex].Vendor);
else if (columnIndex == 2)
result = GetValueFromType(_filterRows[rowIndex].Contract);
else if (columnIndex == 3)
result = _filterRows[rowIndex].RequestNum;
}
else
{
if (columnIndex == 1)
result = GetValueFromType(_filterRows[rowIndex].Receiver);
else if (columnIndex == 2)
result = _filterRows[rowIndex].RequestNum;
}
return result;
}
public void SetValueAt(object aValue, int rowIndex, int columnIndex)
{
}
public object GetObjectAt(int rowIndex, int columnIndex)
{
return null;
}
public event TableModelChangeHandler Change;
#endregion
public void SelectFilter(int columnIndex, string filter)
{
if (columnIndex <= 0)
return;
Collection<RequestListRequestListItem> data;
if (string.IsNullOrEmpty(filter))
{
data = _rows;
}
else
{
data = new Collection<RequestListRequestListItem>();
foreach (var item in _rows)
{
if (_showType == EntityType.MaterialCKOutSelectListNum)
{
if (columnIndex == 1)
{
if (GetValueFromType(item.Vendor).ToString().Contains(filter))
data.Add(item);
}
else if (columnIndex == 2)
{
if (GetValueFromType(item.Contract).ToString().Contains(filter))
data.Add(item);
}
else if (columnIndex == 3)
{
if (item.RequestNum != null && item.RequestNum.Contains(filter))
data.Add(item);
}
}
else
{
if (columnIndex == 1)
{
if (GetValueFromType(item.Receiver).ToString().Contains(filter))
data.Add(item);
}
else if (columnIndex == 2)
{
if (item.RequestNum != null && item.RequestNum.Contains(filter))
data.Add(item);
}
}
}
}
_filterRows.Clear();
_filterRows.AddRange(data);
if (this.Change != null)
this.Change();
}
private object GetValueFromType(EmbedAttrType type)
{
object result = "";
if (type != null && type.Value != null)
result = type.Value;
return result;
}
}
{
#region ITableModel Members
private EntityType _showType;
private Collection<RequestListRequestListItem> _rows;
private List<RequestListRequestListItem> _filterRows;
public RequestListDataSource(RequestList data, EntityType type)
{
_showType = type;
_rows = new Collection<RequestListRequestListItem>();
_filterRows = new List<RequestListRequestListItem>();
if (data != null)
{
foreach (var item in data.RequestListItem)
_rows.Add(item);
}
_filterRows.AddRange(_rows);
}
public int GetRowCount()
{
//RequestList aa;aa.RequestListItem [0].
return _filterRows.Count;
}
public int GetColumnCount()
{
if (_showType == EntityType.MaterialCKOutSelectListNum)
return 4;
else
return 3;
}
public string GetColumnName(int columnIndex)
{
string result = "";
if (columnIndex == 0)
result = "序号";
if (_showType == EntityType.MaterialCKOutSelectListNum)
{
if (columnIndex == 1)
result = "供应商";
else if (columnIndex == 2)
result = "合同号";
else if (columnIndex == 3)
result = "单号";
}
else
{
if (columnIndex == 1)
result = "领用单位";
else if (columnIndex == 2)
result = "单号";
}
return result;
}
public Type GetColumnClass(int columnIndex)
{
if (columnIndex == 0)
return typeof(int);
else
return typeof(String);
}
public bool IsCellEditable(int rowIndex, int columnIndex)
{
return false;
}
public object GetValueAt(int rowIndex, int columnIndex)
{
object result = null;
if (rowIndex >= _filterRows.Count)
return result;
if (columnIndex == 0)
result = rowIndex + 1;
if (_showType == EntityType.MaterialCKOutSelectListNum)
{
if (columnIndex == 1)
result = GetValueFromType(_filterRows[rowIndex].Vendor);
else if (columnIndex == 2)
result = GetValueFromType(_filterRows[rowIndex].Contract);
else if (columnIndex == 3)
result = _filterRows[rowIndex].RequestNum;
}
else
{
if (columnIndex == 1)
result = GetValueFromType(_filterRows[rowIndex].Receiver);
else if (columnIndex == 2)
result = _filterRows[rowIndex].RequestNum;
}
return result;
}
public void SetValueAt(object aValue, int rowIndex, int columnIndex)
{
}
public object GetObjectAt(int rowIndex, int columnIndex)
{
return null;
}
public event TableModelChangeHandler Change;
#endregion
public void SelectFilter(int columnIndex, string filter)
{
if (columnIndex <= 0)
return;
Collection<RequestListRequestListItem> data;
if (string.IsNullOrEmpty(filter))
{
data = _rows;
}
else
{
data = new Collection<RequestListRequestListItem>();
foreach (var item in _rows)
{
if (_showType == EntityType.MaterialCKOutSelectListNum)
{
if (columnIndex == 1)
{
if (GetValueFromType(item.Vendor).ToString().Contains(filter))
data.Add(item);
}
else if (columnIndex == 2)
{
if (GetValueFromType(item.Contract).ToString().Contains(filter))
data.Add(item);
}
else if (columnIndex == 3)
{
if (item.RequestNum != null && item.RequestNum.Contains(filter))
data.Add(item);
}
}
else
{
if (columnIndex == 1)
{
if (GetValueFromType(item.Receiver).ToString().Contains(filter))
data.Add(item);
}
else if (columnIndex == 2)
{
if (item.RequestNum != null && item.RequestNum.Contains(filter))
data.Add(item);
}
}
}
}
_filterRows.Clear();
_filterRows.AddRange(data);
if (this.Change != null)
this.Change();
}
private object GetValueFromType(EmbedAttrType type)
{
object result = "";
if (type != null && type.Value != null)
result = type.Value;
return result;
}
}
其中RequestListDataSource是自己的数据源
在初始化处将自己的数据源转换成
_rows
然后添加过滤方法SelectFilter(int columnIndex, string filter),将过滤后的数据添加到_filterRows,最后绑定的是_filterRows
由此实现了过滤数据作用
附源码