Datagridview CellFormatting问题

时间:2014-06-23 06:35:32

标签: .net datagridview

我正在使用Visual Studio 2012,并创建一个winform桌面应用程序。 我从数据库中获取数据并使用dataview绑定网格。之后,我手动添加2-3列,为记录提供编辑/删除功能。 对于这些编辑/删除列,我使用了DatagridviewImageColumn。在CellFormatting事件中,我正在根据数据加载图像。

问题是我面临以下问题...... 1.此代码在表单Load事件上不能完美运行,不知道为什么???即CellFormatting事件无法正常工作。当我按下刷新按钮(将调用相同的功能)时,网格绑定成功,包括CellFormatting事件代码。 2.当我运行单个表单(客户主数据)时,即使表单加载事件,此代码也能正常工作,但是当我添加与MDI子项相同的表单时,我发现错误!

请帮助

 private void FillCompanies(bool isActive)
    {
        CompanyController CompanyController = new CompanyController();
        try
        {
            this.dgvCompanys.DataSource = DBNull.Value;
            dv = null;
            this.dgvCompanys.Columns.Clear();
            DataTable dt = CompanyController.GetCompanies(isActive);
            dv = dt.DefaultView;
            this.dgvCompanys.VirtualMode = true;
            this.dgvCompanys.DataSource = dv;
            //this.dgvCompanys.AutoGenerateColumns = true;

            DataGridViewImageColumn colEdit = new DataGridViewImageColumn();
            colEdit.HeaderText = "Edit";
            colEdit.Image = Image.FromFile(@"D:\Work\MyPrj\MyPrj\Content\Images\Ico\edit.ico");
            this.dgvCompanys.Columns.Add(colEdit);
            this.dgvCompanys.Columns[gridColEdit].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

            DataGridViewImageColumn colDelete = new DataGridViewImageColumn();
            colDelete.HeaderText = "Delete";
            colDelete.Image = Image.FromFile(@"D:\Work\MyPrj\MyPrj\Content\Images\Ico\delete.ico");
            this.dgvCompanys.Columns.Add(colDelete);
            this.dgvCompanys.Columns[gridColDelete].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            //Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "Images\\RefreshIco_24.png");

            DataGridViewImageColumn colActive = new DataGridViewImageColumn();
            colActive.HeaderText = "Active";
            this.dgvCompanys.Columns.Add(colActive);
            colActive.Image = Image.FromFile(@"D:\Work\MyPrj\MyPrj\Content\Images\Ico\active.ico");
            this.dgvCompanys.Columns[gridColActiveImage].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            this.dgvCompanys.Columns[gridColName].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            this.dgvCompanys.Columns[gridColID].Visible = false;
            this.dgvCompanys.Columns[gridColDataActive].Visible = false;
            this.dgvCompanys.Columns[gridColDataDelete].Visible = false;
            //End With
            this.dgvCompanys.Refresh();
            //dv.RowFilter = "";
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error occurred : " + ex.Message.ToString(), "Application Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

以上是在表单加载时填充网格的代码。下面是表单加载后无法正常工作的代码,但是当我单击刷新按钮并调用相同的代码时,它将正常工作...

private void dgvCompanys_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        bool action = false;
        if (e.ColumnIndex == gridColEdit)
        {
            frmCompany objCompany = new frmCompany(Convert.ToInt64(this.dgvCompanys.Rows[e.RowIndex].Cells[gridColID].Value.ToString()));
            objCompany.ShowDialog();
            action = true;
        }
        else if (e.ColumnIndex == gridColDelete)
        {
            DialogResult dr = MessageBox.Show("Are you sure, you want to delete this record?", "Delete Record?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == System.Windows.Forms.DialogResult.Yes)
            {
                DeleteCompany(Convert.ToInt64(this.dgvCompanys.Rows[e.RowIndex].Cells[gridColID].Value.ToString()), e.RowIndex);
                action = true;
            }
        }
        else if (e.ColumnIndex == gridColActiveImage)
        {
            bool currStatus = Convert.ToBoolean(this.dgvCompanys.Rows[e.RowIndex].Cells[gridColDataActive].Value.ToString());
            DialogResult dr = MessageBox.Show("Are you sure, you want to " + (currStatus == true ? "Deactivate" : "Activate") + " this record?", "Delete Record?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == System.Windows.Forms.DialogResult.Yes)
            {
                InactiveCompany(Convert.ToInt64(this.dgvCompanys.Rows[e.RowIndex].Cells[gridColID].Value.ToString()), currStatus, e.RowIndex);
                action = true;
            }
        }

        if (action)
        {
            RefreshGridFilter();
        }
    }

0 个答案:

没有答案