Click here to Skip to main content
15,881,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am fetching data from a database and displaying data in DataGridView in C#. Now when I right click on any row it should display context menu to edit row. when I press the edit of context menu in the same form it should display all columns of that selected row in TextBox so that I can edit/modify the data.

My code is:

C#
private void dataGrid1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            ContextMenuStrip mnu = new ContextMenuStrip();
            ToolStripMenuItem mnuedit = new ToolStripMenuItem("Edit");
            mnuedit.Click += new EventHandler(editToolStripMenuItem_Click);
            mnu.Items.AddRange(new ToolStripItem[] { mnuedit });
            //Assign to datagridview
            dataGrid1.ContextMenuStrip = mnu;
        }


private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {

            if (dataGrid1.SelectedRows.Count = 0)
            {
                textnotes.Value = dataGrid1.SelectedRows(0).Cells(0).Value;
                textclient.Value = dataGrid1.SelectedRows(0).Cells(1).Value;
                textdatetime.Value = dataGrid1.SelectedRows(0).Cells(2).Value;
                textcombobox.Value = dataGrid1.SelectedRows(0).Cells(3).Value;
            }


        }
Posted
Updated 9-Dec-12 18:22pm
v3
Comments
Ritesh Zaveri 10-Dec-12 0:24am    
Its not working. It shows error in SelectionRows as:
Property or indexer 'Systems.Windows.Form.BaseCollection.Count' cannot be assigned to---it is read only

1 solution

C#
private void dataGrid1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            ContextMenuStrip mnu = new ContextMenuStrip();
            ToolStripMenuItem mnuedit = new ToolStripMenuItem("Edit");
            mnuedit.Click += new EventHandler(editToolStripMenuItem_Click);
            mnu.Items.AddRange(new ToolStripItem[] { mnuedit });
            //Assign to datagridview
            dataGrid1.ContextMenuStrip = mnu;
        }


private void editToolStripMenuItem_Click(object sender, EventArgs e)
{

    if (dataGrid1.SelectedRows.Count == 0)
    {
        textnotes.Text = dataGrid1.SelectedRows[0].Cells[0].Value.ToString();

    }


}



check out this and you are using textnotes.value which is not property for textbox
 
Share this answer
 
v2

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