如何在C#中有多个DataGridViews时返回当前DataGridView的名称 [英] How to return the name of a current DataGridView when you have multiple DataGridViews in C#
问题描述
嗨:
我是C#的新手,很难获得DataGridView的名称。我动态创建了3年的日历。我每天都有一个DataGridView。所以,三年来,我有大约1095个DataGridViews。每个DataGridView都有一个与之关联的名称。名称是日期。
Hi:
I am kind of new at C# and am having difficulty getting the name of a DataGridView. I have created a calendar of 3 years dynamically. I have a DataGridView for each day. So, for three years, I have approximately 1095 DataGridViews. Each DataGridView has a name associated with it. The name is the date.
DataGridView weekDataGridView = new DataGridView();
gridViewName = currentDate.Date.ToString(nameFormat);
String offDate = currentDate.ToString("yyyy-MM-dd");
weekDataGridView.Name = offDate;
因此,对于这个日历,我的姓氏是2016-01-02,因为那是最后一个DataGridView我创建了。
问题是我无法返回我点击的当前DataGridView的名称。
So, for this calendar, my last name is for "2016-01-02" because that is the last DataGridView that I created.
The problem is that I cannot return the name of the current DataGridView that I am clicking on.
weekDataGridView.MouseClick += new MouseEventHandler(weekDataGridView_MouseClick);
private void weekDataGridView_MouseClick(object sender, MouseEventArgs e)
{
weekDataGridView.Focus();
DataGridViewRow row = weekDataGridView.CurrentRow;
string name = GetGridName(weekDataGridView);
}
private string GetGridName(DataGridView dataGridView)
{
string gridViewName = dataGridView.Name;
return gridViewName;
}>/pre>
Every time that I click on a DataGridView my name is 2016-01-02. Even if I click on a DataGridView from Jan 19, 2014. I still get the last DataGridView that was created.
So, I tried this approach after I created all the datagridviews,
<pre lang="c#">weekDataGridView.Name = "";
因此,当我从2014年1月19日点击DataGridView时,我得到了空值;当我点击2016-01-02这是我的预期时,我得到null。因为为DataGridView输入的姓氏是
So, when I click on a DataGridView from January 19, 2014, I get null; I get null when I click on 2016-01-02 which is what I expected. Because the last name entered for the DataGridView is
weekDataGridView.Name = "";
如何返回我点击的weekDataGridView的日期?
非常感谢您的时间和专业知识。
Bosco
How do I return the date of the weekDataGridView that I am click on?
Thank you very much for your time and expertise.
Bosco
推荐答案
sender
参数将为您提供此参数,但首先您需要将genric对象强制转换为DataGridView对象。
像这样例如...
Thesender
parameter will provide this for you, but first you need to cast the genric object to a DataGridView object.
Like this for example ...
DataGridView dgv = (DataGridView)sender;
string dgvName = dgv.Name;
这篇关于如何在C#中有多个DataGridViews时返回当前DataGridView的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!