我涉及的DataGrid使用二

本文介绍了一个使用 C# 编写的 DataGrid 控件中 MouseDown 事件的处理方法,包括如何获取点击位置的信息并进行相应操作,如记录点击的行号和从 DataGrid 中获取特定列的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、dataGrid1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)

ContractedBlock.gifExpandedBlockStart.gif    /DGridMouseDown/#region /DGridMouseDown/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// dataGrid1 MouseDown
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="sender"></param>
ExpandedSubBlockEnd.gif        
/// <param name="e"></param>

InBlock.gif        private void dataGrid1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif                        
InBlock.gif            
//HitTest 方法返回包含所单击区域的行和列的 DataGrid.HitTestInfo 对象
InBlock.gif
            DataGrid myGrid = (DataGrid) sender;
InBlock.gif            System.Windows.Forms.DataGrid.HitTestInfo hti;
InBlock.gif            hti 
= myGrid.HitTest(e.X, e.Y);
InBlock.gif        
InBlock.gif            
switch (hti.Type) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
case System.Windows.Forms.DataGrid.HitTestType.Cell :
InBlock.gif                    
if (e.Clicks==2)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
//记忆当前行号 
InBlock.gif
                        currentERowIndex=hti.Row;
InBlock.gif                        GetSomeFileName(myGrid,hti.Row);
InBlock.gif                        
//myGrid.Select(hti.Row);
ExpandedSubBlockEnd.gif
                    }

InBlock.gif                    
break;
InBlock.gif                
case System.Windows.Forms.DataGrid.HitTestType.RowHeader :
InBlock.gif                
InBlock.gif                    
//记忆当前行号 
InBlock.gif
                    currentERowIndex=hti.Row;
InBlock.gif                    GetSomeFileName(myGrid,hti.Row);
InBlock.gif    
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedBlockEnd.gif        
#endregion

2、
ContractedBlock.gifExpandedBlockStart.gif    /*响应MouseDown/#region /*响应MouseDown/
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 根据DataGrid1 MouseDown,填充下拉列表中的显示数据
InBlock.gif    
/// </summary>
InBlock.gif    
/// <param name="myGrid">dataGrid1</param>
ExpandedSubBlockEnd.gif    
/// <param name="index">选择的行号</param>

InBlock.gifprivate void GetSomeFileName(DataGrid myGrid,int index)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif                acb_YEAR.Text
=myGrid[index,1].ToString();
InBlock.gif                acb_AREA.Text
=myGrid[index,2].ToString();
InBlock.gif    
InBlock.gif    
//提取文件路径
InBlock.gif
                currentEFileName=myGrid[index,8].ToString();
InBlock.gif                
InBlock.gif       
//路径解密
InBlock.gif
    GetELastPath();
InBlock.gif    
InBlock.gif
ExpandedSubBlockEnd.gif}

InBlock.gif        
catch (Exception err)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif    MessageBox.Show(
this,err.Message.ToString(),"信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif        
ExpandedBlockEnd.gif        
#endregion

None.gif

转载于:https://www.cnblogs.com/flashicp/archive/2007/06/06/773929.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值