zoukankan      html  css  js  c++  java
  • C# DataGridView 使用

    之前咩有做个界面的东西,更没有使用过DataGirdView 这个控件。

    现在本来是准备用DeV呢,结果发现我的DEV没有注册,只好暂时用这个DataGridView来替代使用了。


    我现在要是设置两列多行的,一列是属性字段名称值,一列是添加了Combox,下拉表来选择对应的纹理文件路径。

    经过各种搜索查询,终于基本搞定能用。


    一、建了个窗体工程

    给工程来个名字,叫GridViewAndControl,然后来个工具箱里的DataGridView,起来个名字叫m_CAtDataGridView,然后加了个按钮,

    用来保存你选择后对应属性名称和下拉框选择项。

    先说明一下,保存的时候用Hashtable,你懂的,也就是属性名称字段不能重复,否则作为key,是不能存到Hashtable中的啊。

    如下图:简单。



    二、上菜,代码

    有三个事件,窗体加载事件,Form1_Load; 按钮保存,button_click事件;还加了一个添加序号的RowPostPaint,这个网上找到。


    首先,窗体加载:

    	private void Form1_Load(object sender, EventArgs e)
            {
                DataGridViewCheckBoxColumn newColumn = new DataGridViewCheckBoxColumn();
                m_CAtDataGridView.Columns.Insert(0, newColumn);
                newColumn.HeaderText = "选择";            
                DataGridViewTextBoxColumn Texture = new DataGridViewTextBoxColumn();
                Texture.HeaderText = "字段";
                m_CAtDataGridView.Columns.Insert(1, Texture);
                //
                DataGridViewComboBoxColumn dcob = new DataGridViewComboBoxColumn();
                m_CAtDataGridView.Columns.Insert(2, dcob);
                dcob.HeaderText = "纹理路径";            
                dcob.Items.AddRange(new string[] { "Test1", "Test2", "Test3", "Test4" });
    
    
                //foreach (string field in table_Field)
                for (int i = 0; i < 5;i++ )
                {
                    DataGridViewRow newrow = new DataGridViewRow();
                    newrow.CreateCells(m_CAtDataGridView);
                    newrow.Cells[2].Value = "Test2";// 设置默认值
                    newrow.Cells[0].Value = true;
                    newrow.Cells[1].Value = "t" + i.ToString();
                    m_CAtDataGridView.Rows.Add(newrow);
                }            
                dcob.Selected = true;
                // 不显示新添加行
                m_CAtDataGridView.AllowUserToAddRows = false;
                m_CAtDataGridView.AutoSize = false;
                //m_CAtDataGridView.RowHeadersVisible = false;
                // 行颜色变化
                m_CAtDataGridView.RowsDefaultCellStyle.BackColor = Color.FromArgb(255, 90, 0);
                m_CAtDataGridView.AlternatingRowsDefaultCellStyle.BackColor = Color.FromArgb(50, 205, 50);
                m_CAtDataGridView.GridColor = Color.FromArgb(16, 139, 87);
            }

    其次,按钮保存表里的值,保存到Hashtable中。

    	private void button1_Click(object sender, EventArgs e)
            {
                Hashtable HashFieldTex = new Hashtable();
                int iNum = m_CAtDataGridView.Rows.Count;
    
                try 
                {
                    for (int i = 0; i < iNum; i++)
                    {
                        string Chos = m_CAtDataGridView.Rows[i].Cells[1].Value.ToString();
                        string Textrue = m_CAtDataGridView.Rows[i].Cells[2].Value.ToString();
                        HashFieldTex.Add(Chos, Textrue);
                    }
                }
                catch (Exception ex)
                {
                    string mes = ex.Message;
                }
            }

    最后,给第一列添加序列号。

    	private void m_CAtDataGridView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
            {
                using (SolidBrush b = new SolidBrush(m_CAtDataGridView.RowHeadersDefaultCellStyle.ForeColor))
                    e.Graphics.DrawString((e.RowIndex + 1).ToString(),
                        e.InheritedRowStyle.Font, b, e.RowBounds.Location.X, e.RowBounds.Location.Y); 
            }

    最后效果:不美,因为没有把写给列 设置宽度大小!


    代码全景:

    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;
    using System.Collections;
    
    namespace GridViewAndContorl
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                DataGridViewCheckBoxColumn newColumn = new DataGridViewCheckBoxColumn();
                m_CAtDataGridView.Columns.Insert(0, newColumn);
                newColumn.HeaderText = "选择";            
                DataGridViewTextBoxColumn Texture = new DataGridViewTextBoxColumn();
                Texture.HeaderText = "字段";
                m_CAtDataGridView.Columns.Insert(1, Texture);
                //
                DataGridViewComboBoxColumn dcob = new DataGridViewComboBoxColumn();
                m_CAtDataGridView.Columns.Insert(2, dcob);
                dcob.HeaderText = "纹理路径";            
                dcob.Items.AddRange(new string[] { "Test1", "Test2", "Test3", "Test4" });
    
                //foreach (string field in table_Field)
                for (int i = 0; i < 5;i++ )
                {
                    DataGridViewRow newrow = new DataGridViewRow();
                    newrow.CreateCells(m_CAtDataGridView);
                    newrow.Cells[2].Value = "Test2";// 设置默认值
                    newrow.Cells[0].Value = true;
                    newrow.Cells[1].Value = "t" + i.ToString();
                    m_CAtDataGridView.Rows.Add(newrow);
                }            
                dcob.Selected = true;
                // 不显示新添加行
                m_CAtDataGridView.AllowUserToAddRows = false;
                m_CAtDataGridView.AutoSize = false;
                //m_CAtDataGridView.RowHeadersVisible = false;
                // 行颜色变化
                m_CAtDataGridView.RowsDefaultCellStyle.BackColor = Color.FromArgb(255, 90, 0);
                m_CAtDataGridView.AlternatingRowsDefaultCellStyle.BackColor = Color.FromArgb(50, 205, 50);
                m_CAtDataGridView.GridColor = Color.FromArgb(16, 139, 87);
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Hashtable HashFieldTex = new Hashtable();
                int iNum = m_CAtDataGridView.Rows.Count;
    
                try 
                {
                    for (int i = 0; i < iNum; i++)
                    {
                        string Chos = m_CAtDataGridView.Rows[i].Cells[1].Value.ToString();
                        string Textrue = m_CAtDataGridView.Rows[i].Cells[2].Value.ToString();
                        HashFieldTex.Add(Chos, Textrue);
                    }
                }
                catch (Exception ex)
                {
                    string mes = ex.Message;
                }
            }
    
            private void m_CAtDataGridView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
            {
                using (SolidBrush b = new SolidBrush(m_CAtDataGridView.RowHeadersDefaultCellStyle.ForeColor))
                    e.Graphics.DrawString((e.RowIndex + 1).ToString(),
                        e.InheritedRowStyle.Font, b, e.RowBounds.Location.X, e.RowBounds.Location.Y); 
            }
        }
    }
    




    新手新学,见效就笑吧!

    代码免费下载:http://download.csdn.net/detail/cartzhang/5741101 

    源码免分下载


  • 相关阅读:
    27. Remove Element
    26. Remove Duplicates from Sorted Array
    643. Maximum Average Subarray I
    674. Longest Continuous Increasing Subsequence
    1. Two Sum
    217. Contains Duplicate
    448. Find All Numbers Disappeared in an Array
    566. Reshape the Matrix
    628. Maximum Product of Three Numbers
    UVa 1349 Optimal Bus Route Design (最佳完美匹配)
  • 原文地址:https://www.cnblogs.com/qitian1/p/6461991.html
Copyright © 2011-2022 走看看