问题描述
大家好,
我一直在生成动态表单并添加动态控件及其方法.
我想通过单击具有相同形式的按钮控件来访问Gridview控件.你们能帮我吗?
这是代码:
Hi guys,
I have been generating a dynamic form and adding dynamic controls and it''s methods.
I want to access the Gridview control on click of the button control that are in the same form. Can you guys just help me out?
Here is the code :
//gridview event :
private void dgvorders_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
try
{
DataGridView mygrid = sender as DataGridView;
}
catch(exception ex)
{}
}
//Button event :
private void btnsubmit_Click(object sender, EventArgs e)
{
Button mybutton = sender as Button;
//Here i want to access the gridview and fetch the selected rows
}
网格控件,按钮控件和表单都在运行时动态生成.
谢谢...:)
Grid control, button control and form all are dynamically generated at runtime.
Thanks... :)
推荐答案
private void btnsubmit_Click(object sender, EventArgs e)
{
try
{
for (int i = 0; i < dgvorders.Rows.Count; i++)
{
if (dgvorders.Rows[i].Cells["orderstatus"].Value.ToString() == "True")
{
masterdatagrid.Rows.Add(dgvorders.Rows[i]);
}
else { }
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error on Submit");
}
Orders.Close();
}
它给了我一个例外:
当控件与数据绑定时,不能以编程方式将行添加到DataGridView的行集合中."
帮帮我...:confused:
阿舒托什·贾因(Ashutosh Jain).
it gives me the exception:
" Rows cannot be programmatically added to the DataGridView''s rows collection when the control is data-bound."
Help me out guys... :confused:
Ashutosh Jain.
这篇关于单击按钮控件,以动态生成的形式访问Datagridview控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!