DataGridView SelectionChanged EVENT [英] DataGridView SelectionChanged EVENT

查看:80
本文介绍了DataGridView SelectionChanged EVENT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



在我的表格上有两个datagridview:



一个datagridview被填充DataBinding:



Hello,

On my form a have two datagridview:

One datagridview is filled by DataBinding:

private void controlContractePrincipalLoad(object sender, EventArgs e)
       {
           string NumeClientCautat = txtCautaDupaNume.Text;
           this.sClienti.DataSource = SetariAmanet.TotiClienti(NumeClientCautat);
       }





一个CellContentClick EVENT(它的作品):





A CellContentClick EVENT (its works):

private void dVizualizareClientiCellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridView vizualizare = sender as DataGridView;
            if (vizualizare != null)
            {
                IdClientSelectat = Convert.ToInt32(vizualizare.SelectedRows[0].Cells[0].Value.ToString());
                if (IdClientSelectat != 0)
                {
                    InformatiiDespreClientContracte();
                }
            }
        }





和SelectionChanged事件:





AND a SelectionChanged event:

private void dVizualizareClientiSelectionChanged(object sender, EventArgs e)
        {
            DataGridView vizualizare = sender as DataGridView;
            if (vizualizare != null)
            {
                IdClientSelectat = Convert.ToInt32(vizualizare.SelectedRows[0].Cells[0].Value.ToString());
                if (IdClientSelectat != 0)
                {
                    InformatiiDespreClientContracte();
                }
            }
        }





SelectionChanged事件不起作用

错误按摩是:索引超出范围。



SelectionChanged事件执行两次(我认为),我的IdClientSelectat第一次使用值,然后他在这一行给我一个错误:





The SelectionChanged event is not working
The error massage is: Index out of range.

The SelectionChanged event is executed two times (i think), the first time my IdClientSelectat hase a value, then he gives me an error at this line:

IdClientSelectat = Convert.ToInt32(vizualizare.SelectedRows[0].Cells[0].Value.ToString());





问题出在哪里?



Where is the problem?

推荐答案

选择更改事件多次发生 - 其中一次是控制时间首先创建,然后才能获得任何数据。此时,您使用绝对值会导致超出范围异常,因为没有可以使用的行,也没有列。



添加检查:

The Selection changed event happens a number of times - and one of those times is when the control is first created, and before it gets any data. At this point, your use of absolute values causes an "out of range" exception because there are no rows to work with, and no columns yet either.

Add checking:
if (vizualizare != null && vizualizare.Rows.Count > 0 && vizualizare.Columns.Count > 0)


这篇关于DataGridView SelectionChanged EVENT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆