国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
C# DataGridView中實(shí)現(xiàn)類似ComboBox可編輯的下拉框功能 - 清凈的雨天...

Google了一下, 原來這個(gè)問題很多人碰到過,也有一些解決方案.不過感覺大多不好,有些許的問題.也許有好的,我沒找到吧.于是在前人基礎(chǔ)上,小小的改動(dòng)了下.
目前還有兩個(gè)小問題待解決:
1) DataGridView最后一行,第一列的ComboBox輸入值,DataGridView應(yīng)該自動(dòng)增加一行(編輯其他列會(huì)自動(dòng)增加一行).
2) 第一列的ComboBox中,假設(shè)其Items中有Shenzhen, 我輸入Shen,Cell中的Value將會(huì)時(shí)Shen. 當(dāng)再次點(diǎn)擊ComboBox,顯示下拉Item時(shí),Item中的Shenzhen會(huì)高亮顯示,ComboBox的Text會(huì)自動(dòng)變成Shenzhen. 其實(shí)這也是ComboxBox的一個(gè)功能.可能有時(shí)不需要這個(gè)功能.

 

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
//此Form中有兩個(gè)控件,一個(gè)DataGridView(dataGridView1),一個(gè)ComboBox(comboBox1)
//DataGridView中有兩列(都是DataGridViewTextBoxColumn的),其中第一列實(shí)現(xiàn)類似ComboBox的功能
//第一列中只有正在編輯的Cell才會(huì)顯示ComboBox,其他的不會(huì)顯示,這樣好看一點(diǎn)
namespace Test
{
    
public partial class Form1 : Form
    
{
        
private int comboBoxColumnIndex = 0// DataGridView的首列

        
public Form1()
        
{
            InitializeComponent();
            InitComboBoxValues();
            
this.dataGridView1.Controls.Add(this.comboBox1); 
            
this.dataGridView1.CellEnter += new DataGridViewCellEventHandler(dataGridView1_CellEnter);
            
this.dataGridView1.CellLeave+=new DataGridViewCellEventHandler(dataGridView1_CellLeave);
        }


        
private void InitComboBoxValues()
        
{
            
this.comboBox1.Items.AddRange(new String[] "Beijing""Shanghai""Guangzhou""Wuhan""Shenzhen" });
            
this.comboBox1.AutoCompleteMode = AutoCompleteMode.Suggest; //輸入提示
            this.comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;
        }


        
private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
        
{
            
if (e.ColumnIndex == comboBoxColumnIndex)
            
{
                
//此處cell即CurrentCell
                DataGridViewCell cell = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
                Rectangle rect 
= this.dataGridView1.GetCellDisplayRectangle(cell.ColumnIndex, cell.RowIndex, true);
                
this.comboBox1.Location = rect.Location;
                
this.comboBox1.Size = rect.Size;
                comfirmComboBoxValue(
this.comboBox1, (String)cell.Value);
                
this.comboBox1.Visible = true;             
            }

        }


        
private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
        
{
            
if (e.ColumnIndex == comboBoxColumnIndex)
            
{
                
//此處cell不為CurrentCell
                DataGridViewCell cell = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]; 
                cell.Value 
= this.comboBox1.Text;
                
this.comboBox1.Visible = false;
            }

        }


        
private void comfirmComboBoxValue(ComboBox com, String cellValue)
        
{
            com.SelectedIndex 
= -1;
            
if (cellValue == null)
            
{
                com.Text 
= "";
                
return;
            }

            com.Text 
= cellValue;
            
foreach (Object item in com.Items)
            
{
                
if ((String)item == cellValue)
                
{
                    com.SelectedItem 
= item;
                }

            }

        }

    }

}

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
(轉(zhuǎn))使用DataGridView控件常見問題解答 - 邊寫邊唱 - 博客園
c# 如何用datagridview獲取選中的單元格內(nèi)容
C#如何實(shí)現(xiàn)DataGridView到DataGridView的拖拽
C#中DatagridView單元格動(dòng)態(tài)綁定控件
DataGridView DataGridViewCheckBoxColumn編輯時(shí)實(shí)時(shí)觸發(fā)事件
DataGridView內(nèi)包含ComboBox列 實(shí)現(xiàn)選定ComboBox的項(xiàng)數(shù)據(jù)的聯(lián)動(dòng).
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服