精品熟人妻一区二区三区四区不卡-精品爽黄69天堂a-精品水蜜桃久久久久久久-精品丝袜国产自在线拍-精品丝袜国产自在线拍a-精品丝袜国产自在线拍免费看

LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

C# dataGridView 自動生成幾行幾列及手動輸入整型字符

admin
2025年4月22日 21:32 本文熱度 65

C# dataGridView生成12號4列的表格

private void Form1_Load(object sender, EventArgs e)
{
    // 清除默認(rèn)列
    dataGridView1.Columns.Clear();
    
    // 添加4列(首列為序號列)
    dataGridView1.Columns.Add("ColIndex", "序號");
    dataGridView1.Columns.Add("Col2", "列2");
    dataGridView1.Columns.Add("Col3", "列3");
    dataGridView1.Columns.Add("Col4", "列4");

    // 添加12行數(shù)據(jù)
    for (int i = 0; i < 12; i++)
    {
        int rowIndex = dataGridView1.Rows.Add();
        dataGridView1.Rows[rowIndex].Cells[0]().Value = i + 1; // 首列填充1-12
    }
}

第一行的特殊處理

// 在循環(huán)中添加條件判斷
if (rowIndex > 0) // 跳過第一行(索引0)
{
    dataGridView1.Rows[rowIndex].Cells[0]().Value = rowIndex; // 從1開始填充
}

自動生成行號(行頭顯示)

// 在RowPostPaint事件中繪制行號
private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
    var grid = sender as DataGridView;
    using (SolidBrush brush = new SolidBrush(grid.RowHeadersDefaultCellStyle.ForeColor))
    {
        // 行號位置微調(diào)
        e.Graphics.DrawString(
            (e.RowIndex + 1).ToString(),
            grid.DefaultCellStyle.Font,
            brush,
            new PointF(e.RowBounds.X + 20, e.RowBounds.Y + 4)
        );
    }
}

關(guān)鍵配置

禁止自動生成列:dataGridView1.AutoGenerateColumns = false; 禁止選中首行:dataGridView1.ClearSelection(); 列寬自適應(yīng):

dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

手動輸入整型字符

完整代碼如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace VMPro2024._08.WinForm
{
    public partial class CalibrationForm : Form
    {
        public CalibrationForm()
        {
            InitializeComponent();
            正極平臺_dataGridView.CellValidating += 正極平臺_dataGridView_CellValidating;

        }

        private void CalibrationForm_Load(object sender, EventArgs e)
        {
            //設(shè)置4列
            正極平臺_dataGridView.ColumnCount = 4;
            正極像素_dataGridView.ColumnCount = 4;

            負(fù)極平臺_dataGridView.ColumnCount = 4;
            負(fù)極像素_dataGridView.ColumnCount = 4;

            //添加12行空數(shù)據(jù)
            for (int i = 0; i < 12; i++)
            {
                int indexRow1 = 正極平臺_dataGridView.Rows.Add();
                正極平臺_dataGridView.Rows[i].Cells[0].Value = indexRow1 + 1;

                int indexRow2 = 正極像素_dataGridView.Rows.Add();
                正極像素_dataGridView.Rows[i].Cells[0].Value = indexRow2 + 1;

                int indexRow3 = 負(fù)極平臺_dataGridView.Rows.Add();
                負(fù)極平臺_dataGridView.Rows[i].Cells[0].Value = indexRow3 + 1;

                int indexRow4 = 負(fù)極像素_dataGridView.Rows.Add();
                負(fù)極像素_dataGridView.Rows[i].Cells[0].Value = indexRow4 + 1;
            }

            正極平臺_dataGridView.RowHeadersWidth = 60;
            正極像素_dataGridView.RowHeadersWidth = 60;

            負(fù)極平臺_dataGridView.RowHeadersWidth = 60;
            負(fù)極像素_dataGridView.RowHeadersWidth = 60;

            //允許直接編輯
            正極平臺_dataGridView.EditMode = DataGridViewEditMode.EditOnKeystroke;
            正極像素_dataGridView.EditMode = DataGridViewEditMode.EditOnKeystroke;
            負(fù)極平臺_dataGridView.EditMode = DataGridViewEditMode.EditOnKeystroke;
            負(fù)極像素_dataGridView.EditMode = DataGridViewEditMode.EditOnKeystroke;
            //強(qiáng)制取消第一列的只讀屬性
            正極平臺_dataGridView.Columns[1].ReadOnly = false;
        }

        private void 負(fù)極平臺_dataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {

        }

        private DataGridViewTextBoxEditingControl cellEdit;//聲明編輯控件

        /// <summary>
        /// 通過事件限制輸入類型
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 正極平臺_dataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (正極平臺_dataGridView.CurrentCell.ColumnIndex == 1
                || 正極平臺_dataGridView.CurrentCell.ColumnIndex == 2 ||
                正極平臺_dataGridView.CurrentCell.ColumnIndex == 3)
            {
                TextBox tb = e.Control as TextBox;
                if (tb != null)
                {
                    tb.KeyPress += new KeyPressEventHandler(Tb_KeyPress);
                }
            }
        }

        private void Tb_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '-' && e.KeyChar != (char)Keys.Back)
            {
                e.Handled = true;// 阻止無效輸入 
                MessageBox.Show("僅允許輸入整數(shù)!");
            }
            else
            {
                e.Handled = false;
                正極平臺_dataGridView.Rows[正極平臺_dataGridView.CurrentCell.RowIndex].ErrorText = "";
            }

            // 負(fù)號只能出現(xiàn)在首位
            if (e.KeyChar == '-' && ((TextBox)sender).SelectionStart != 0)
            {
                e.Handled = true;
            }
        }

        /// <summary>
        /// 在單元格結(jié)束編輯時(shí)驗(yàn)證輸入內(nèi)容是否為整數(shù):
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 正極平臺_dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (正極平臺_dataGridView.IsCurrentCellInEditMode)
            {
                if (e.ColumnIndex == 1 || e.ColumnIndex == 2 || e.ColumnIndex == 3)
                {
                    //單元格值為空時(shí)觸發(fā)
                    string input = e.FormattedValue.ToString();
                    if (!string.IsNullOrEmpty(input) && !int.TryParse(e.FormattedValue.ToString(), out _))
                    {
                        e.Cancel = true;
                        MessageBox.Show("輸入內(nèi)容必須為整數(shù)!");
                    }
                    else
                    {
                        e.Cancel = false;
                    }
                }
            }
        }

        private void 正極平臺_dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (正極平臺_dataGridView.EditingControl is TextBox tb)
            {
                tb.KeyPress -= Tb_KeyPress;
            }

            //清除錯誤提示
            正極平臺_dataGridView.Rows[e.RowIndex].ErrorText = "";
        }       
    }
}

效果圖:


閱讀原文:原文鏈接


該文章在 2025/4/23 10:52:51 編輯過
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點(diǎn)晴ERP是一款針對中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國內(nèi)大量中小企業(yè)的青睞。
點(diǎn)晴PMS碼頭管理系統(tǒng)主要針對港口碼頭集裝箱與散貨日常運(yùn)作、調(diào)度、堆場、車隊(duì)、財(cái)務(wù)費(fèi)用、相關(guān)報(bào)表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點(diǎn),圍繞調(diào)度、堆場作業(yè)而開發(fā)的。集技術(shù)的先進(jìn)性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點(diǎn)晴WMS倉儲管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質(zhì)期管理,貨位管理,庫位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號管理軟件。
點(diǎn)晴免費(fèi)OA是一款軟件和通用服務(wù)都免費(fèi),不限功能、不限時(shí)間、不限用戶的免費(fèi)OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved

主站蜘蛛池模板: 亚洲暴爽av天天爽日日碰 | 日本高清中文字幕在线 | 色色色色色色视频 | 亚洲一本之道高清在线观看 | 国产日韩一区二区三区 | 日韩一区二区三区影片 | 日本一区四区不卡视频 | 91精品免费不卡在线观看 | 丰满的瑜伽老师bd三级 | 精品國產福利第一區二區三區 | 欧美无人区码 | 亚洲日韩在线中文字幕线路 | 成年无码专区在线蜜芽tv | 人妻丰满熟av无码区hd | 精品国产乱码一区二区三区麻豆 | 国产精品亚洲专区无码不卡 | 精品人妻少妇一区二区三区 | 无码精品人妻一区二区三区av | 日本一区二区三区免费 | 高清喜剧片大全 | 岛国av无码专区免费看 | 日韩欧美中文字幕第 | 国产91亚洲中文天堂在线观 | 国产高清免费av | 国产a∨天天免费观看美女w | 性欧美高清视频在线观看 | 亚洲成a∧人片在 | 国精产品999国精产品官网 | 亚洲av不卡无码国产粉色 | 天天看片亚洲欧美国产 | 国产亚洲精品美女久久久 | 精品麻豆色欲色欲色欲w | 尤物tv国产精品看片在线 | 狠狠色婷婷狠狠狠亚洲综合 | 91视频app污版ios| 国产全肉乱妇杂乱视频 | 国产免国产免‘费 | 自拍偷自拍亚洲精品情侣 | 日本经典电影在线观看 | 亚洲av鲁丝一区二区三区 | 99久久免费精品国产72精品九 |