写代码的麒麟臂 2017-04-12 09:56 采纳率: 50%
浏览 803
已采纳

ItemClicked出现NullReferenceException,请问怎么回事?

private void formOrderManagement_Load(object sender, EventArgs e)
{
try
{
LoadMenu(ultraGridCustomer);
ultraGridCustomer.DataSource = m_WebService.LoadAllCustomers();
this.ultraGridCustomer.KeyActionMappings.Add(new GridKeyActionMapping(Keys.Enter, UltraGridAction.CommitRow, UltraGridState.RowFirst, UltraGridState.Row, 0, 0));
}
catch (Exception ex)
{
MessageBox.Show("Error occurred when loading customer, the error message is: " + ex.ToString());
}
}
private void customerMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
try
{
if (e.ClickedItem.Text == "Add")
{

                ultraGridCustomer.DisplayLayout.Override.AllowAddNew = AllowAddNew.TemplateOnBottom;
                ultraGridCustomer.DisplayLayout.Override.CellClickAction = CellClickAction.EditAndSelectText;
                customerMenuStrip.Items["Edit"].Enabled = false;
                customerMenuStrip.Items["Delete"].Enabled = false;
                customerMenuStrip.Items["Add"].Visible = false;
                return;
            }
            else
            {
                customerMenuStrip.Items["Edit"].Enabled = true;
                customerMenuStrip.Items["Delete"].Enabled = true;
                customerMenuStrip.Items["Add"].Visible = true;
            }
            if (e.ClickedItem.Text == "Edit")
            {
                ultraGridCustomer.DisplayLayout.Override.CellClickAction = CellClickAction.EditAndSelectText;
                ultraGridCustomer.DisplayLayout.Override.AllowAddNew = AllowAddNew.Yes;
                customerMenuStrip.Items["Add"].Enabled = false;
                customerMenuStrip.Items["Delete"].Enabled = false;
                customerMenuStrip.Items["Edit"].Visible = false;
                return;
            }
            else
            {
                customerMenuStrip.Items["Add"].Enabled = true;
                customerMenuStrip.Items["Delete"].Enabled = true;
                customerMenuStrip.Items["Edit"].Visible = true;
            }
            if (e.ClickedItem.Text == "Browser")
            {
                ultraGridCustomer.DisplayLayout.Override.CellClickAction = CellClickAction.RowSelect;
                ultraGridCustomer.DisplayLayout.Override.AllowAddNew = AllowAddNew.Yes;
                customerMenuStrip.Items["Add"].Enabled = true;
                customerMenuStrip.Items["Edit"].Enabled = true;
                customerMenuStrip.Items["Delete"].Enabled = true;
                customerMenuStrip.Items["Add"].Visible = true;
                customerMenuStrip.Items["Edit"].Visible = true;
                customerMenuStrip.Items["Delete"].Visible = true;

            }
            if (e.ClickedItem.Text == "Delete")
            {

                if (ultraGridCustomer.Selected.Rows.Count > 0 && !string.IsNullOrWhiteSpace(ultraGridCustomer.ActiveRow.Cells["customer_id"].Value.ToString()))
                {
                    if (MessageBox.Show("Please confirm your operation, delete " + ultraGridCustomer.ActiveRow.Cells["customer_first_name"].Value.ToString() + "?", "Attention", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        bool isDeleted;
                        isDeleted = m_WebService.DeleteCustomerById(Convert.ToInt32(ultraGridCustomer.ActiveRow.Cells["customer_id"].Value));
                        if (isDeleted)
                        {
                            ultraGridCustomer.DeleteSelectedRows(false);
                            ultraGridCustomer.Refresh();
                            MessageBox.Show("Delete successful.");
                        }
                        else
                        {
                            MessageBox.Show("Delete failed.");
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Data error, please restart the application.");
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error occurred when item clicked, the error message is: " + ex.ToString());
        }
    }
            private void ultraGridCustomer_MouseDown(object sender, MouseEventArgs e)
    {
        try
        {
            UltraGridRow row;
            Infragistics.Win.UIElement element;
            if (e.Button == MouseButtons.Right)
            {
                element = ultraGridCustomer.DisplayLayout.UIElement.ElementFromPoint(e.Location);
                row = element.GetContext(typeof(UltraGridRow)) as UltraGridRow;
                if (row != null && row.IsDataRow)
                {
                    ultraGridCustomer.ActiveRow = row;
                    customerMenuStrip.Show(MousePosition.X, MousePosition.Y);
                }
                return;
            }
        }
                            private void LoadMenu(Control control)
    {
        customerMenuStrip.Items.Add("Add");
        customerMenuStrip.Items.Add("Edit");
        customerMenuStrip.Items.Add("Delete");
        customerMenuStrip.Items.Add("Browse");
    }

  • 写回答

1条回答 默认 最新

  • devmiao 2017-04-12 15:48
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已采纳回答 4月8日

悬赏问题

  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题