简体   繁体   中英

Pass data from one form to a specific cell in a datagridview from another form in C#

I have a datagridview, dgv1, that has 5 columns namely: Credit, Debit, EntryDescription, AccountingEntry, Employee. since in the EntryDescription I may have to input several lines of text, what I did is make an event wherein when the EntryDescription is clicked, a new form with a richtextbox appears as ShowDialog and as soon as writing the description is done, user presses the AddDescription button and whatever texts that have been entered in the richtextbox should be added to the dgv1's current row and EntryDescription Cell. I have some codes crated already but I do not know to continue the rest. I Need your wisdom. Thanks.

I know clicking the EntryDescription and a dropdown textbox appears wherein I place the description is good and better way, but I do not know how to do it so I just settled with this.

In the AccountingEntry Form

public static int RowCount { get; set; }
    public static string DescriptionToAdd { get; set; }
private void dgv1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {
        if (e.ColumnIndex != EntryDescription.Index) return;
        var bal = new AddAccountingEntryDescription
        {
            RowIndex = dgv1.CurrentCell.RowIndex
        };
        bal.ShowDialog();
    }

In the AddAccountingEntryDescription Form

public int RowIndex { get; set; }
 private void btnAdd_Click(object sender, EventArgs e)
    {
        AccountingEntry.RowCount = RowIndex;
        AccountingEntry.DescriptionToAdd = txtDescriptionToAdd.Text;
    }

This is very simple. The text entry form doesn't have to know anything about the grid or even the other form. All it has to do is expose the data via a public property. After ShowDialog returns, the original form gets the data from that property and does whatever it wants with it. If you want to put that text into a cell in the grid then do so just as you would any other time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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