在DataGridView中编辑数据 [英] Edit data in DataGridView

查看:103
本文介绍了在DataGridView中编辑数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从数据库中获取数据并在C#中的DataGridView中显示数据。现在,当我右键单击任何一行时,它应显示上下文菜单以编辑行。当我按相同的形式按下上下文菜单的编辑时,它应该显示TextBox中所选行的所有列,以便我可以编辑/修改数据。



我的代码是:



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:

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;
            }


        }

推荐答案

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();

    }


}







看看这个,你使用的是textnotes.value,它不是textbox的属性




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


这篇关于在DataGridView中编辑数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆