Click here to Skip to main content
15,888,218 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have a datagridview in the main form. By clicking on a particular column of datagridview, it will open the sub form or ownedform(dynamically created). The owned form have a listbox and an OK button. When The user selects an item from listbox and click OK then selected text needs to be appear on the datagridview cell. But when i click OK i couldn't get the result. I am not receiving any error neither.Here's the code

NOTE: If use Textbox instead of listbox, i am able to pass the value to datagridview. I just don't know the way to handle listbox to pass value.

CODE FOR DYNAMICALLY CREATING OWNED FORM & ADDING CONTROLS

private List


BUTTON CLICK EVENT HANDLER - NOT WORKING (LISTBOX)

private void dbtn_Click_SIGTB(object sender, EventArgs e)
        {
            int rowidx = dataGridView3.CurrentCell.RowIndex;
            int colidx = dataGridView3.CurrentCell.ColumnIndex;
            foreach(ListBox lb in inputlistboxes)
            {
                string selected = lb.GetItemText(lb.SelectedItem);
                dataGridView2.Rows[rowidx].Cells[colidx].Value = selected;
            }
            
            foreach (Form frm in ownform)
               frm.Close();
        }


BUTTON EVENT HANDLER - WORKING (TEXTBOX)

private void dbtn_Click_VARTB(object sender, EventArgs e)
        {
            int rowidx = dataGridView2.CurrentCell.RowIndex;
            int colidx = dataGridView2.CurrentCell.ColumnIndex;
            foreach (TextBox txt in inputTextBoxes)
                dataGridView2.Rows[rowidx].Cells[colidx].Value = txt.Text;
            foreach (Form frm in ownform)
                frm.Close();
        }


DATAGRIDVIEW CELL CONTENT CLICK

private void dataGridView3_CellContentClick(object sender, DataGridViewCellEventArgs e)
       {
           if (dataGridView3.CurrentCell.ColumnIndex.Equals(3))
           {
               ShowMyOwnedForm_SIGTB();
           }
       }


What I have tried:

private void dbtn_Click_SIGTB(object sender, EventArgs e)
{
int rowidx = dataGridView3.CurrentCell.RowIndex;
int colidx = dataGridView3.CurrentCell.ColumnIndex;
foreach(ListBox lb in inputlistboxes)
{
string selected = lb.GetItemText(lb.SelectedItem);
dataGridView2.Rows[rowidx].Cells[colidx].Value = selected;
}

foreach (Form frm in ownform)
frm.Close();
}
Posted
Updated 18-Sep-16 22:45pm
Comments
Member 12226114 18-Sep-16 21:42pm    
SORRY MISSED OUT THE OWNED FORM CODE. ADDING HERE,

private List

1 solution

hi just update your dbtn_Click_SIGTB function as follows,
C#
private void dbtn_Click_SIGTB(object sender, EventArgs e)
{
    int rowidx = dataGridView3.CurrentCell.RowIndex;
    int colidx = dataGridView3.CurrentCell.ColumnIndex;
    foreach(ListBox lb in inputlistboxes)
    {
        string selected = lb.Items[lb.SelectedIndex].ToString(); //lb.GetItemText(lb.SelectedItem);
        dataGridView2.Rows[rowidx].Cells[colidx].Value = selected;
    }

    foreach (Form frm in ownform)
       frm.Close();
}
 
Share this answer
 
Comments
Member 12226114 23-Sep-16 0:54am    
hi, still not updating. any other suggestions pls?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900