【问题标题】:datagridView edits数据网格视图编辑
【发布时间】:2011-06-16 19:43:32
【问题描述】:

我有一个绑定到 SQLite 数据库的 datagridview。现在,我想修改 datagridview 中的一些字段。

有 4 个 TEXT 列 - 时间戳、消息、类型、哈希。 现在我找到一行,我想右键单击它.. 它应该有选项 - “包含”.. 所以,当我在上下文菜单中单击包含时,我的 DGV 的类型列应该从不管它以前是什么...(我不希望它被启用编辑..我只是希望它在程序中更改)我如何获取我单击的索引并访问该特定单元格以修改它?

【问题讨论】:

    标签: c# .net datagridview


    【解决方案1】:

    这段代码做你想做的事:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
    
            BindingList<User> users = new BindingList<User>();
            users.Add(new User(){Name = "Fred", Included = "False", Title="Mr"});
            users.Add(new User(){Name = "Sue", Included = "False", Title="Dr"});
            users.Add(new User(){Name = "Jack", Included = "False", Title="Mr"});
    
            dataGridView1.DataSource = users;
        }
    
    
        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);
    
                if (hit.rowIndex >= 0)
                {
                    dataGridView1.ClearSelection();
                    dataGridView1.Rows[hit.RowIndex].Selected = true;
                    contextMenuStrip1.Show(this.dataGridView1, new Point(e.X, e.Y));
                }
            }
        }
    
        private void includeToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            // Included was the name of the column to change in my example code,
            // you could also use the index of the column if you know it.
            dataGridView1.SelectedRows[0].Cells["Included"].Value = "Included";
        }
    }
    
    public class User
    {
        public string Name { get; set; }
        public string Title { get; set; }
        public string Included { get; set; }
    }
    

    我想不出比实际使用 DataGridView 的选定行属性更好的方法来通知上下文菜单选择了哪一行 - 您也可以将其存储在类级别字段中,但我不认为很整洁。

    【讨论】:

    • 我试过这个.. 但我得到这个错误 索引超出范围。必须是非负数且小于集合的大小。参数名称:index 还有 'dataGridView1.SelectedRows[0].Cells["Included"].Value = "Included";'单元格中的“包含”在这里是什么意思?
    • @techmanc 包含的是您要编辑的列的名称。如果您知道,您也可以拥有该列的索引。我还编辑了代码以处理索引错误 - 这可能是由于右键单击标题所致。
    • 处理错误的代码究竟在哪里编辑?右键单击标题,如?我仍然收到错误消息! :(
    • 我已经添加了索引检查。去c#房间聊天吧,也许我可以帮你更多。
    【解决方案2】:
    private void dataGridView_DoubleClick(object sender, EventArgs e)
    {  
        var grid = (DataGridView)sender;
        var point = grid.PointToClient(Cursor.Position);
        var hit = grid.HitTest(p.X, p.Y);      
        MessageBox.Show(string.Format("{0} / {1}", hit.ColumnIndex, hit.RowIndex));
    }
    

    代码未经编译测试,但理论上这应该可以完成工作。


    dataGridView.Rows[rowIndex].Cells[cellIndex].Value = "something";

    【讨论】:

    • 我可以获得点击位置的索引。现在我如何编辑 DGV 的那个单元格?
    • 我已经更新了我的答案,但以后你应该熟悉 MSDN 上的文档。没那么难-msdn.microsoft.com/en-US/library/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多