首页  ·  知识 ·  云计算
.net中关于DataGrid的常见问题
佚名  http://www.51blog.net  综合  编辑:dezai  图片来源:网络
using System;using System.Drawing;using System.Collections;u
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Data.OleDb;

namespace filedemo
{
///
/// Form1 的摘要说明。
///

public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.DataGrid dataGrid1;
private OleDbConnection conn;
DataSet dataSet1 =null;

System.Windows.Forms.DataGridTableStyle dataGridTableStyle1;
///
/// 必需的设计器变量。
///

private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();


//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

///
/// 清理所有正在使用的资源。
///

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///

private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(104, 232);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// dataGrid1
//
this.dataGrid1.Anchor = (((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, 8);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(280, 152);
this.dataGrid1.TabIndex = 1;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.dataGrid1,
this.button1});
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);

}
#endregion

///
/// 应用程序的主入口点。
///

[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
string strconn= @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=G:\bbs.mdb" ;
OleDbDataAdapter adapter = new OleDbDataAdapter("select top 10 * from sheetbbs order by id desc ",strconn);
dataSet1 = new DataSet();
adapter.Fill(dataSet1,"sheetbbs");


dataGridTableStyle1 = new System.Windows.Forms.DataGridTableStyle();
DataGridTextBoxColumn dataGridTextBoxColumn1 = new DataGridTextBoxColumn();
DataGridTextBoxColumn dataGridTextBoxColumn2 = new DataGridTextBoxColumn();
DataGridTextBoxColumn dataGridTextBoxColumn3 = new DataGridTextBoxColumn();
//dataGridTableStyle1.DataGrid = this.dataGrid1;
dataGridTableStyle1.MappingName =this.dataSet1.Tables[0].TableName ;
// dataGridTableStyle1.ForeColor = System.Drawing.Color.Red;

dataGridTableStyle1.GridLineColor = Color.Blue;
dataGridTextBoxColumn1.MappingName = "ID";
dataGridTextBoxColumn1.HeaderText ="art id";

dataGridTextBoxColumn2 .MappingName ="userID";
dataGridTextBoxColumn2.HeaderText ="User ID";
dataGridTextBoxColumn2.TextBox.ForeColor =Color.Green;
dataGridTextBoxColumn2.TextBox.DoubleClick += new EventHandler(this.changeColor);
dataGridTextBoxColumn3 .MappingName ="title";
dataGridTextBoxColumn3.HeaderText ="Title";
//
// PropertyDescriptorCollection pcol = this.BindingContext[this.dataSet1.Tables[0]].GetItemProperties();
//
//
// DataGridColumnStyle csOrderAmount =
// new DataGridTextBoxColumn(pcol["content"], "c", true);
DataGridTextBoxColumn csOrderAmount = new DataGridTextBoxColumn();

csOrderAmount.MappingName = "content";

csOrderAmount.HeaderText ="内容";
csOrderAmount.TextBox.WordWrap = true;


DataGridColumnStyle website = new DataGridTextBoxColumn();
website.MappingName = "website";
website.HeaderText ="webSite";

dataGridTableStyle1.GridColumnStyles.AddRange(new
System.Windows.Forms.DataGridColumnStyle[] {
dataGridTextBoxColumn1,
dataGridTextBoxColumn2,
dataGridTextBoxColumn3,
website,
csOrderAmount
});
dataGrid1.TableStyles.Add(dataGridTableStyle1);
dataGrid1.DataSource = this.dataSet1.Tables["sheetbbs"];

}

private void Form1_Load(object sender, System.EventArgs e)
{

}

private void changeColor(object sender, System.EventArgs e)
{
//MessageBox.Show(sender.GetType().ToString());
this.dataGrid1.Select(this.dataGrid1.CurrentRowIndex);
this.dataGrid1.TableStyles["sheetbbs"].SelectionBackColor=Color.White;
this.dataGrid1.TableStyles["sheetbbs"].SelectionForeColor = Color.Red;
// this.dataGrid1.SelectionBackColor=Color.White;
// this.dataGrid1.SelectionForeColor = Color.Red;
//this.dataGrid1.sele

foreach(DataGridTextBoxColumn col in this.dataGrid1.TableStyles["sheetbbs"].GridColumnStyles)
{
col.TextBox.ForeColor = Color.Green;
}
this.dataGrid1.TableStyles["sheetbbs"].ForeColor = Color.Gold;
}
}
}
本文作者:佚名 来源:http://www.51blog.net
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读