DataGridView的一项功能
DataGridView有没有这样的功能,当点击其中的一行时,获取该行的数据,用于填充编辑界面。这个功能相信大家都做过。
说说思路,最好给点代码实例。。。 --------------------编程问答-------------------- DataGridView1.CurrentRow获取当前单元格所在的行。 --------------------编程问答--------------------
++
这个功能就是跟编辑该行是一样的,获取数据,添加显示到其他控件上 --------------------编程问答-------------------- 补个, --------------------编程问答--------------------
同意这个 --------------------编程问答-------------------- 通常的做法应该是:取得在datagridview中的行的主键,然后将相应记录显示在详细视图中,在编辑保存详细视图。 --------------------编程问答-------------------- 事件cellclick和cellcontentclick
前者点击行时触发,后者点击行的内容时发生,区别,如果改行某个列为null,前者可以触发,后者不触发
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex < 0 || e.RowIndex >= this.dataGridView1.Rows.Count - 1)
return;
this.listBox1.Items.Clear();
try
{
eindex = e.RowIndex;
string racebh = this.dataGridView1.Rows[e.RowIndex].Cells["match_ID"].Value.ToString();//获取当前行match_ID列的数据
RaceBh = racebh;
//做相关处理。
}
catch (Exception ex)
{ MessageBox.Show(ex.Message); }
}
--------------------编程问答-------------------- 这个很不错,支持 --------------------编程问答-------------------- 顶6楼!居然加了这个判断
if (e.RowIndex < 0 || e.RowIndex >= this.dataGridView1.Rows.Count - 1)
记得想当年我第一次写C#的时候就没加~~囧~~~ --------------------编程问答-------------------- 直接使用:
dataGridView1.CurrentRow获取当前单元格所在的行。 --------------------编程问答-------------------- 学习,吸收。 --------------------编程问答-------------------- 6楼正解 --------------------编程问答-------------------- 学习。。。。。。。。。。。 --------------------编程问答-------------------- --------------------编程问答-------------------- --------------------编程问答--------------------
注意不错哦。 --------------------编程问答-------------------- 注意积累。 --------------------编程问答-------------------- 可以使用Binding --------------------编程问答--------------------
--------------------编程问答--------------------
namespace WindowsFormsApplication1
{
partial class MainForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.grdView = new System.Windows.Forms.DataGridView();
this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Column3 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.txtName = new System.Windows.Forms.TextBox();
this.txtCode = new System.Windows.Forms.TextBox();
this.label8 = new System.Windows.Forms.Label();
this.label9 = new System.Windows.Forms.Label();
this.txtRemark = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.bindingSource1 = new System.Windows.Forms.BindingSource(this.components);
((System.ComponentModel.ISupportInitialize)(this.grdView)).BeginInit();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.bindingSource1)).BeginInit();
this.SuspendLayout();
//
// grdView
//
this.grdView.AllowUserToAddRows = false;
this.grdView.AllowUserToDeleteRows = false;
this.grdView.AutoGenerateColumns = false;
this.grdView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.grdView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.Column1,
this.Column2,
this.Column3});
this.grdView.Dock = System.Windows.Forms.DockStyle.Fill;
this.grdView.Location = new System.Drawing.Point(3, 17);
this.grdView.Name = "grdView";
this.grdView.ReadOnly = true;
this.grdView.RowTemplate.Height = 23;
this.grdView.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
this.grdView.Size = new System.Drawing.Size(255, 354);
this.grdView.TabIndex = 1;
//
// Column1
//
this.Column1.DataPropertyName = "Code";
this.Column1.HeaderText = "编码";
this.Column1.Name = "Column1";
this.Column1.ReadOnly = true;
//
// Column2
//
this.Column2.DataPropertyName = "Name";
this.Column2.HeaderText = "名称";
this.Column2.Name = "Column2";
this.Column2.ReadOnly = true;
//
// Column3
//
this.Column3.DataPropertyName = "Remark";
this.Column3.HeaderText = "备注";
this.Column3.Name = "Column3";
this.Column3.ReadOnly = true;
//
// txtName
//
this.txtName.Location = new System.Drawing.Point(41, 38);
this.txtName.Name = "txtName";
this.txtName.Size = new System.Drawing.Size(153, 21);
this.txtName.TabIndex = 14;
//
// txtCode
//
this.txtCode.Location = new System.Drawing.Point(41, 13);
this.txtCode.Name = "txtCode";
this.txtCode.Size = new System.Drawing.Size(153, 21);
this.txtCode.TabIndex = 12;
//
// label8
//
this.label8.AutoSize = true;
this.label8.Location = new System.Drawing.Point(6, 42);
this.label8.Name = "label8";
this.label8.Size = new System.Drawing.Size(29, 12);
this.label8.TabIndex = 13;
this.label8.Text = "名称";
//
// label9
//
this.label9.AutoSize = true;
this.label9.Location = new System.Drawing.Point(6, 17);
this.label9.Name = "label9";
this.label9.Size = new System.Drawing.Size(29, 12);
this.label9.TabIndex = 11;
this.label9.Text = "编码";
//
// txtRemark
//
this.txtRemark.Location = new System.Drawing.Point(41, 63);
this.txtRemark.Multiline = true;
this.txtRemark.Name = "txtRemark";
this.txtRemark.Size = new System.Drawing.Size(153, 300);
this.txtRemark.TabIndex = 15;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(6, 66);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(29, 12);
this.label1.TabIndex = 16;
this.label1.Text = "备注";
//
// groupBox1
//
this.groupBox1.Controls.Add(this.grdView);
this.groupBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.groupBox1.Location = new System.Drawing.Point(0, 0);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(261, 374);
this.groupBox1.TabIndex = 17;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "列表";
//
// groupBox2
//
this.groupBox2.Controls.Add(this.txtName);
this.groupBox2.Controls.Add(this.label9);
this.groupBox2.Controls.Add(this.label1);
this.groupBox2.Controls.Add(this.label8);
this.groupBox2.Controls.Add(this.txtRemark);
this.groupBox2.Controls.Add(this.txtCode);
this.groupBox2.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBox2.Location = new System.Drawing.Point(261, 0);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(200, 374);
this.groupBox2.TabIndex = 18;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "明细";
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(461, 374);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.groupBox2);
this.Name = "MainForm";
this.Text = "Binding";
this.Load += new System.EventHandler(this.MainForm_Load);
((System.ComponentModel.ISupportInitialize)(this.grdView)).EndInit();
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.bindingSource1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.DataGridView grdView;
private System.Windows.Forms.TextBox txtName;
private System.Windows.Forms.TextBox txtCode;
private System.Windows.Forms.Label label8;
private System.Windows.Forms.Label label9;
private System.Windows.Forms.DataGridViewTextBoxColumn Column1;
private System.Windows.Forms.DataGridViewTextBoxColumn Column2;
private System.Windows.Forms.DataGridViewTextBoxColumn Column3;
private System.Windows.Forms.TextBox txtRemark;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.BindingSource bindingSource1;
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class MainForm : Form
{
DataTable dt;
DataColumn dc;
DataRow dr;
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
dt = new DataTable("Demo");
//创建并添加虚拟列到虚拟表中
dc = new DataColumn("Name");
//指定字段的数据类型,这步没有也不会出错
dc.DataType = System.Type.GetType("System.String");
dt.Columns.Add(dc);
dc = new DataColumn("Code");
dc.DataType = System.Type.GetType("System.String");
dt.Columns.Add(dc);
dc = new DataColumn("Remark");
dc.DataType = System.Type.GetType("System.String");
dt.Columns.Add(dc);
dr = dt.NewRow();
dr["Name"] = "软件工程师";
dr["Code"] = "0001";
dr["Remark"] = "测试数据0001";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Name"] = "测试工程师";
dr["Code"] = "0002";
dr["Remark"] = "测试数据0002";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Name"] = "实施工程师";
dr["Code"] = "0003";
dr["Remark"] = "测试数据0003";
dt.Rows.Add(dr);
//将数据绑定到控件里显示出来
grdView.DataSource = bindingSource1;
bindingSource1.DataSource = dt;
txtName.DataBindings.Add(new Binding("Text", bindingSource1, "Name", true, DataSourceUpdateMode.OnPropertyChanged));
txtCode.DataBindings.Add(new Binding("Text", bindingSource1, "Code", true, DataSourceUpdateMode.OnPropertyChanged));
txtRemark.DataBindings.Add(new Binding("Text", bindingSource1, "Remark", true, DataSourceUpdateMode.OnPropertyChanged));
}
}
}
补充:.NET技术 , C#