在datagrid中我选中了几行,我如何得到每一行的值.
解决方案 »
- c# 做的图书馆管理系统无法再其他人电脑上无法添加数据
- 在文件上右键启动程序问题
- C#中使用combox控件保存 key/value的问题
- 请教高人关于AD域用户查找及邮件组的问题
- 数组问题,怎么释放数组空间
- Execl和sql server 的格式转换问题
- 怎么样在2000下实现office2003的效果阿?
- [精华]高仿QQ2013局域通讯版(无服务器,无数据库,立登开聊)
- 在VS2010里打包有数据库的C#程序,然后提示无法找到文件“*”的源文件,说我的“.mdf”和“.ldf”文件可能不存在或已被锁定,怎么办啊~
- 如何判断某个文件是否已经打开
- 让我郁闷的C#中的Merge方法……
- 为什么在lisbox中单击取消选中后,会自动选中第一项
System.Data.DataTable myTable = dsDataSet.Tables[0].Copy();
myTable.Clear();
myTable.Rows.Add(dsDataSet.Tables[0].Rows[myDataGrid.CurrentRowIndex].ItemArray);
这样的话,占用内存很大,而且也只能是没有排序的.使用isselect的方法要比你的简单
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text;namespace Zhzuo.DBWindowsApplication
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button btnFillData;
private System.Windows.Forms.Button btnShowSelectedRow;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
InitializeComponent();
this.dbCategories = new DataTable("Categories");
this.dataGrid1.DataSource = dbCategories;
this.bindingmanager = this.BindingContext[this.dataGrid1.DataSource];
this.bindingmanager.PositionChanged += new EventHandler(bindingmanager_PositionChanged);
} private DataTable dbCategories;
private BindingManagerBase bindingmanager = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.btnFillData = new System.Windows.Forms.Button();
this.btnShowSelectedRow = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(8, 40);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(488, 168);
this.dataGrid1.TabIndex = 0;
//
// btnFillData
//
this.btnFillData.Location = new System.Drawing.Point(8, 232);
this.btnFillData.Name = "btnFillData";
this.btnFillData.TabIndex = 1;
this.btnFillData.Text = "数据填充";
this.btnFillData.Click += new System.EventHandler(this.btnFillData_Click);
//
// btnShowSelectedRow
//
this.btnShowSelectedRow.Location = new System.Drawing.Point(104, 232);
this.btnShowSelectedRow.Name = "btnShowSelectedRow";
this.btnShowSelectedRow.TabIndex = 2;
this.btnShowSelectedRow.Text = "显示选中行";
this.btnShowSelectedRow.Click += new System.EventHandler(this.btnShowSelectedRow_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(504, 269);
this.Controls.Add(this.btnShowSelectedRow);
this.Controls.Add(this.btnFillData);
this.Controls.Add(this.dataGrid1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false); }
#endregion
//填充数据网格
private void btnFillData_Click(object sender, System.EventArgs e)
{
//填充事例数据到DataGrid
NorthwindDbAccess.FillCategories(dbCategories);
} //留着以后用
private void bindingmanager_PositionChanged(object sender, EventArgs e)
{
//DataRowView drv = (DataRowView)this.bindingmanager.Current;
//DataRow dr = drv.Row;
} //显示选中的多行,对DataGrid列进行排序后选择也适用
private void btnShowSelectedRow_Click(object sender, System.EventArgs e)
{
StringBuilder sb = new StringBuilder();
for(int i = 0;i < dataGrid1.VisibleRowCount;i++)
{
int position = this.bindingmanager.Position;
if(this.dataGrid1.IsSelected(i))
{
this.bindingmanager.Position = i;
DataRowView drv = (DataRowView)this.bindingmanager.Current;
DataRow dr = drv.Row;
sb.AppendFormat("{0} {1} {2}\n",dr[0],dr[1],dr[2]);
}
this.bindingmanager.Position = position;
}
//打印选择数据
MessageBox.Show(sb.ToString());
}
}
}