简体   繁体   English

C#将datagridview从一种形式读取到另一种形式

[英]C# reading datagridview from one form to another form

I have two forms, one contains a datagridview control with a data source from MS Access. 我有两个表单,一个包含带有MS Access数据源的datagridview控件。 Data is properly displayed on the datagridview. 数据正确显示在datagridview上。 First I select a cell in that data grid view, then I obtain the Row Index of the currently selected cell. 首先,我在该数据网格视图中选择一个单元格,然后获取当前所选单元格的行索引。 I use that Row Index in an accessor function (code is in the main form): 我在访问器函数中使用该行索引(代码在主窗体中):

public String Name
{ 
    get 
    { 
        return dataGridView1.Rows[selrow].Cells[1].Value.ToString(); 
    } 
}

selrow contains the row index of the currently selected cell. selrow包含当前所选单元格的行索引。 Next, I click a button "Edit record" and this it displays my 2nd form as a modal form. 接下来,我单击“编辑记录”按钮,这将显示我的第二个表单作为模式表单。 I want to display the value of the above accessor in a text box, so code goes like this (code is in the 2nd form): 我想在文本框中显示上述访问者的值,所以代码是这样的(代码是第二种形式):

private void EditRecord_Load(object sender, EventArgs e)
{
    CashLoan main = new CashLoan();
    txtEName.Text = main.name;
}

But when I try to run and debug, I get this "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index" directed at my accessor function. 但是当我尝试运行和调试时,我得到这个“索引超出范围。必须是非负的且小于集合的大小。参数名称:索引”指向我的访问器函数。 I can't seem to find the source of the problem. 我似乎无法找到问题的根源。 Thank you in advance. 先感谢您。

Try 尝试

public String Name
{ 
    get 
    { 
        return dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[1].Value.ToString(); 
    } 
}

cause i guess you have a problem with the current selected row index. 因为我猜你当前选择的行索引有问题。 Also it would be possible that your row have only one cell. 您的行也可能只有一个单元格。 In that case replace the 1 with a 0 cause index starts at 0 (so 0 = 1, 1 = 2 etc...) 在这种情况下,用0原因索引替换1从0开始(所以0 = 1,1 = 2等...)

dataGridView1.Rows[selrow].Cells[0].Value.ToString(); 

If I understand you want show value from main form in the textbox of Editrecord form. 如果我理解你想在Editrecord表单的文本框中显示主表单的显示值。 If so 如果是这样的话

In Load handler you create new instance of main form( CashLoan ). Load handler中,您可以创建主窗体的新实例( CashLoan )。
And trying read values from empty datagridview(I assume you not filling datagridview with data in the contructor) of main form. 并尝试从主数据的空datagridview(我假设您没有使用构造函数中的数据填充datagridview)读取值。

Try pass your value to EditRecord form as parameter in contructor: 尝试将值传递给EditRecord表单作为contructor中的参数:
In EditRecord form: 在EditRecord形式:

private String _originalValue;
public EditRecord(String originalValue)
{
    _originalValue = originalValue;
}
//Then in Load handler
private void EditRecord_Load(object sender, EventArgs e)
{
    this.txtEName.Text = _originalValue;
}

//In main form
EditRecord frm = new EditRecord(this.name);
frm.ShowDialog();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM