c#中如何定义内存表绑定DataGrid的简单实例

实例代码如下 :

using  System;
using  System.Drawing;
using  System.Collections;
using  System.ComponentModel;
using  System.Windows.Forms;
using  System.Data;

namespace  DataGridStyle
{
    
/// <summary>
    
/// Form1 的摘要说明。
    
/// </summary>

    public class DemoForm : System.Windows.Forms.Form
    
{
        
private System.Windows.Forms.DataGrid dgd;
        
private System.Windows.Forms.DataGridTableStyle dataGridTableStyle1;
        
private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn1;
        
private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn2;
        
private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn3;
        
private System.Windows.Forms.DataGridBoolColumn dataGridBoolColumn1;
        
private System.Windows.Forms.ComboBox cboDepts;
        
/// <summary>
        
/// 必需的设计器变量。
        
/// </summary>

        private System.ComponentModel.Container components = null;

        DataTable dt;

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

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


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

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

            }

            
base.Dispose( disposing );
        }


        
Windows 窗体设计器生成的代码

        
/// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>

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


        
private void DemoForm_Load(object sender, System.EventArgs e)
        
{
            dt
=new DataTable(); //创建内存表
            dt.Columns.Add("Id",typeof(int));//定义内存表的字段
            dt.Columns.Add("Name",typeof(string));
            dt.Columns.Add(
"Dept",typeof(string));
            dt.Columns.Add(
"IsMarried",typeof(bool));

            dt.Columns[
"IsMarried"].AllowDBNull=false;
            dt.Columns[
"IsMarried"].DefaultValue=true;

            DataRow dr
=dt.NewRow();
            dr[
"Id"]=9922;
            dr[
"Name"]="Mike";
            dr[
"Dept"]="开发部";
            dr[
"IsMarried"]=false;
            dt.Rows.Add(dr);
    
            dgd.DataSource
=dt;

            dataGridTextBoxColumn3.TextBox.Controls.Add(cboDepts);
//在DataGrid中添加下拉选择框
            
        }


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

            
if (dgd.CurrentRowIndex>=dt.Rows.Count)
                dgd[dgd.CurrentCell]
=cboDepts.Text;
            
else
                dt.Rows[dgd.CurrentRowIndex][
"Dept"]=cboDepts.Text;
        }


        
private void dgd_CurrentCellChanged(object sender, System.EventArgs e)
        
{
            
if (dgd.CurrentCell.ColumnNumber==2) cboDepts.Text=dgd[dgd.CurrentCell].ToString();
        }

    }

}

这个实例讲述了内存表的使用以及如何绑定DataGrid,还有就是为DataGrid添加一些其他的控件,如下拉选择框,选择框等等,让DataGrid更加丰富

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值