DataGrid经过排序、增、删或用DataView过滤记录后,如何找到当前行。
我现在试过两种方法都不行,请高手们指点一下。
1.
int i = DG.CurrentRowIndex;
this.Text = DS.Tables[0].Rows[i][0].ToString();
结果:经过排序、增、删或用DataView过滤记录后,出来的行都不正确。
2.
DataRowView dv = (DataRowView)this.BindingContext[DS, DS.Tables[0].TableName].Current; DataRow dr = dv.Row;
this.Text = dr[0].ToString();
这种结果更郁闷,总是显示第一行。我是刚学C#的菜鸟,请大侠们指点一下吧!
我现在试过两种方法都不行,请高手们指点一下。
1.
int i = DG.CurrentRowIndex;
this.Text = DS.Tables[0].Rows[i][0].ToString();
结果:经过排序、增、删或用DataView过滤记录后,出来的行都不正确。
2.
DataRowView dv = (DataRowView)this.BindingContext[DS, DS.Tables[0].TableName].Current; DataRow dr = dv.Row;
this.Text = dr[0].ToString();
这种结果更郁闷,总是显示第一行。我是刚学C#的菜鸟,请大侠们指点一下吧!
//显示选中的多行,对DataGrid列进行排序后选择也适用
private void btnShowSelectedRow_Click(object sender, System.EventArgs e)
{
CurrencyManager currencyManager = (CurrencyManager)this.BindingContext[this.dataGrid1.DataSource,this.dataGrid1.DataMember];
StringBuilder sb = new StringBuilder();
for(int i = 0;i < dataGrid1.VisibleRowCount;i++)
{
if(this.dataGrid1.IsSelected(i))
{
DataRowView drv = (DataRowView)currencyManager.List[i];
DataRow dr = drv.Row;
sb.AppendFormat("{0} {1} {2}\n",dr[0],dr[1],dr[2]);
}
}
//打印选择数据
MessageBox.Show(sb.ToString());
}
//根据DataRow [] drs 选择DataGrid中对应的行
private void btnShowSelectedRow_Click(object sender, System.EventArgs e)
{
DataRow [] drs = DataTable.Select("条件");
CurrencyManager currencyManager = (CurrencyManager)this.BindingContext[this.dataGrid1.DataSource,this.dataGrid1.Memeber];
//for(int i = 0;i < dataGrid1.VisibleRowCount;i++)
for(int i = 0;i < currencyManager.Count;i++)
{
DataRowView drv = (DataRowView)currencyManager.List[i];
DataRow dr = drv.Row;
if(Array.IndexOf(drs,dr) != -1)
{
this.dataGrid1.Select(i);
}
}
}
//获取不同版本的DataRow
using System;
using System.IO;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Collections;
using System.Data;
using System.Xml;
using System.Management;
using System.Net;
namespace Zhzuo
{
class ZZConsole
{
[STAThread]
static void Main(string[] args)
{
DataSet ds = new DataSet();
CreatDataSetSchema(ds);
InitData(ds);
DataRow drdel = ds.Tables["Hosts"].Rows[0];
drdel.Delete();
Console.WriteLine(drdel.HasVersion(DataRowVersion.Default).ToString());
foreach(DataRow dr in ds.Tables["Hosts"].GetChanges(DataRowState.Deleted).Rows)
{
//if(dr.HasVersion(DataRowVersion.Current))
//{
Console.WriteLine((string)dr["HId"]);
Console.WriteLine(dr["IsLocal",DataRowVersion.Original].ToString());
//}
}
Console.WriteLine("end");
Console.ReadLine();
}
//初始化数据集结构
private static void CreatDataSetSchema(DataSet ds)
{
DataTable dt = new DataTable("Hosts");
DataColumn dc = new DataColumn("HId",typeof(String));
dt.Columns.Add(dc);
dc = new DataColumn("IsLocal",typeof(Boolean));
dt.Columns.Add(dc);
ds.Tables.Add(dt);
}
//加入数据
private static void InitData(DataSet ds)
{
DataRow hostsRow = ds.Tables["Hosts"].NewRow();
hostsRow["HId"] = "192.192.132.229";
hostsRow["IsLocal"] = true;
ds.Tables["Hosts"].Rows.Add(hostsRow); hostsRow = ds.Tables["Hosts"].NewRow();
hostsRow["HId"] = "192.192.132.231";
hostsRow["IsLocal"] = false;
ds.Tables["Hosts"].Rows.Add(hostsRow); hostsRow = ds.Tables["Hosts"].NewRow();
hostsRow["HId"] = "192.192.132.233";
hostsRow["IsLocal"] = false;
ds.Tables["Hosts"].Rows.Add(hostsRow);
}
}
}