更新货币管理器头寸

发布于 2024-10-22 01:31:08 字数 1037 浏览 7 评论 0原文

我有一个在 DataGridView 上使用的 ContextMenuStrip,DataGridView 位于 SplitContainer 面板内部。我的用户请求他们能够右键单击网格中的任何行,然后他们右键单击的行将成为选定的行,并且菜单将出现。我一直在工作的代码,直到我将 DataGridView 放置在 SplitContainer 面板内

private void DataGridView_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            // Get the row that was right-clicked on
            DataGridView.HitTestInfo hitTestInfo = DataGridView.HitTest(e.X, e.Y);
            if (hitTestInfo != DataGridView.HitTestInfo.Nowhere)
            {
                // Change the binding source position to the new row to 'select' it
                BindingSource.CurrencyManager.Position = hitTestInfo.RowIndex;
            }
        }
    }

一切似乎都工作正常,直到到达最后一行

BindingSource.CurrencyManager.Position = hitTestInfo.RowIndex;

即使 hitTestInfo.RowIndexposition 始终保持在 -1 code> 尝试分配不同的值。这可能是因为 SplitContainer 面板吗?如果是这样,有关于如何修复它的建议吗?

谢谢

I have a ContextMenuStrip that is used on a DataGridView, the DataGridView is inside of a SplitContainer panel. My users have requested that they be able to right click on any of the rows in the grid and the row they right-click on will then become the selected row and the menu will appear. The code that I have has been working, until I placed the DataGridView inside of the SplitContainer Panel

private void DataGridView_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            // Get the row that was right-clicked on
            DataGridView.HitTestInfo hitTestInfo = DataGridView.HitTest(e.X, e.Y);
            if (hitTestInfo != DataGridView.HitTestInfo.Nowhere)
            {
                // Change the binding source position to the new row to 'select' it
                BindingSource.CurrencyManager.Position = hitTestInfo.RowIndex;
            }
        }
    }

Everything seems to be working fine until it reaches the last line

BindingSource.CurrencyManager.Position = hitTestInfo.RowIndex;

The Position always stays at -1, even if the hitTestInfo.RowIndex has a different value it is trying to assign. Could this be because of the SplitContainer Panel? If so, any suggestions on how to fix it?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

み零 2024-10-29 01:31:08

问题是您必须通过(DataGridView 的)BindingContext 访问CurrencyManager 才能获取正确的BindingManager。我将您的源代码替换为 (dataGridView1.BindingContext[dataGridView1.DataSource] asCurrencyManager) ,它的工作原理就像一个魅力。以下是进行此更改的完整事件处理程序。我的 DataGridView 名称是 dataGridView1

    private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
    {
         if (e.Button == MouseButtons.Right)        
         {           
             // Get the row that was right-clicked on            
             DataGridView.HitTestInfo hitTestInfo = dataGridView1.HitTest(e.X, e.Y);            
             if (hitTestInfo != DataGridView.HitTestInfo.Nowhere)            
             {                
                 // Change the binding source position to the new row to 'select' it                
                 (dataGridView1.BindingContext[dataGridView1.DataSource] as CurrencyManager).Position  = hitTestInfo.RowIndex;            
             }       
         }
    }

The problem is you've to access the CurrencyManager through BindingContext (of DataGridView) to get the correct BindingManager. I took your source code just replaced BindingSource.CurrencyManager with (dataGridView1.BindingContext[dataGridView1.DataSource] as CurrencyManager) and it works like a charm. Following is the full event handler with this change. My DataGridView name is dataGridView1.

    private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
    {
         if (e.Button == MouseButtons.Right)        
         {           
             // Get the row that was right-clicked on            
             DataGridView.HitTestInfo hitTestInfo = dataGridView1.HitTest(e.X, e.Y);            
             if (hitTestInfo != DataGridView.HitTestInfo.Nowhere)            
             {                
                 // Change the binding source position to the new row to 'select' it                
                 (dataGridView1.BindingContext[dataGridView1.DataSource] as CurrencyManager).Position  = hitTestInfo.RowIndex;            
             }       
         }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文