日韩欧美人妻无码精品白浆,www.大香蕉久久网,狠狠的日狠狠的操,日本好好热在线观看

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

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

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

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;
            //強制取消第一列的只讀屬性
            正極平臺_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é)束編輯時驗證輸入內(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)
                {
                    //單元格值為空時觸發(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)文章
正在查詢...
點晴ERP是一款針對中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國內(nèi)大量中小企業(yè)的青睞。
點晴PMS碼頭管理系統(tǒng)主要針對港口碼頭集裝箱與散貨日常運作、調(diào)度、堆場、車隊、財務(wù)費用、相關(guān)報表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點,圍繞調(diào)度、堆場作業(yè)而開發(fā)的。集技術(shù)的先進(jìn)性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點晴WMS倉儲管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質(zhì)期管理,貨位管理,庫位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號管理軟件。
點晴免費OA是一款軟件和通用服務(wù)都免費,不限功能、不限時間、不限用戶的免費OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved

h一区二区日韩| 最牛AV在线播放| 国产嫩鸡巴视频| 欧美亚洲国产主播精品| 久久亚洲欧洲视频| 亚洲 欧洲av在线| 又黄又嫩又爽视频| 午夜福利内涵图| 啪啪啪免费国产99| 欧美日韩一区二区三区小说| 欧美激情日韩激情国产激情| 国产欧美一区二区 - 百度| 久久电影日韩| 淫荡熟女乱伦欧美一区二区三区| 久久久婷婷婷婷婷| 欧美一区二区在线播放的| 日本三级中出人妻电影| 97伦理色色| 91麻豆产精品久久久久| 中国极品少妇XXXXX| 久久熟妇一区二区| 无码福利久久| 欧美双泬同入视频| 国产不卡精品91| 啊嗯好舒服啊视频在线观看| 久久天天草天天干精品| 亚洲大鸡巴干熟女| 嗯啊哦好湿| 中文音像视频一区二区| 日本免费一区二区二十一区| 日韩一区在线ab| 香蕉视频亚洲精品| 超碰2018| 爆操 高潮 久久| 在线午夜免费激情啊啊啊| 17艹午夜| 91麻豆国产电影在线观看 | 黄色三级片久久久久| 久久小说下载| 午夜中文字幕欧美精品下载| 字幕 欧美 国产 精品|