文本未出现在DataGridViewButtonColumn上 [英] Text not appearing on DataGridViewButtonColumn

查看:55
本文介绍了文本未出现在DataGridViewButtonColumn上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我对DataGridView控件中的按钮单元格有一个非常烦人的问题.我在运行时将网格绑定到数据集.网格中的某些行将链接到pdf文档.我创建一个按钮列并将其添加到网格中,然后根据另一列的值在按钮列中设置单元格的文本.当我单步执行代码时,我可以看到按钮列的ColumnIndex为10,并且该代码正确设置了我想要的按钮单元格的文本.但是,当表单出现时,我想要的行的按钮文本值为空白.当我单击按钮时,我检查CellContentClick事件以查看ColumnIndex是否为10(这是按钮列),它告诉我ColumnIndex为0,即使它是最后一列.然后,当我重新加载网格时,我再次调用BindHistoryGrid方法,如果该列存在,它将删除该列并重新添加它.这次,它正确设置了按钮文本.是否发生一些我看不到的奇怪行为?第一次添加时如何将ColumnIndex按钮设置为10(即使它告诉我它是10)?

Hi all, I have a really annoying issue with a button cell in a DataGridView control. I'm binding the grid to a dataset at runtime. Some of the rows in the grid will be linked to pdf documents. I create a button column and add it to the grid, and set the text of the cell in the button column based on the value of another column. When I step through the code I can see the ColumnIndex of the button column is 10, and the code correctly sets the text of the button cells I want. However when the form appears, the button text values for the rows I want are blank. When I click the button I check in the CellContentClick event to see if the ColumnIndex is 10 (which is the button column) it tells me the ColumnIndex is 0, even though it's the last column. Then when I reload the grid I call the BindHistoryGrid method again which drops the column if it exists and re-adds it. This time it sets the button text correctly. Is there some strange behavior going on that I can't see? How do I set the button ColumnIndex to 10 the first time I add it (even though it tells me that it's 10)?

    private DataGridViewButtonColumn PDFButtonColumn;

private void BindHistoryGrid()
{
dataGridViewStmt.DataSource = ah.getAccountHistory(0, dateTimePicker1.Value, dateTimePicker2.Value);

if (dataGridViewStmt.Columns["GetPDFFile"] != null)
dataGridViewStmt.Columns.Remove("GetPDFFile");

dataGridViewStmt.Columns[0].DisplayIndex = 0;
dataGridViewStmt.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridViewStmt.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
dataGridViewStmt.Columns[0].Visible = false;
dataGridViewStmt.Columns[1].Visible = false;
dataGridViewStmt.Columns.Add(PDFButtonColumn);

dataGridViewStmt.RowHeadersVisible = false;
dataGridViewStmt.ReadOnly = true;
dataGridViewStmt.AllowUserToAddRows = false;

foreach (DataGridViewRow row in dataGridViewStmt.Rows)
{
//if (((string)row.Cells[5].Value).Contains("Invoice"))
if (((int)row.Cells[9].Value) > 0)
{
((DataGridViewButtonCell)(row.Cells[10])).Value = "Get Invoice";
}
else
{
((DataGridViewButtonCell)(row.Cells[10])).Value = "";
}
}
}


private void dataGridViewStmt_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 10 && dataGridViewStmt.CurrentRow.Cells[6].Value != System.DBNull.Value)
{
string pdfFile = "";
int docID = 0;

pdfFile = (string)dataGridViewStmt.CurrentRow.Cells[5].Value + ".pdf";
docID = (int)dataGridViewStmt.CurrentRow.Cells[9].Value;

if (docID > 0)
{
getPDFFile(docID, pdfFile, "pdf");
}
else
{
MessageBox.Show("No invoice available for this item";
}
}
}

推荐答案

您好,

如果您使用的是精美代码,请使用

私有DataGridViewButtonColumn PDFButtonColumn ;会引发异常.

我检查了一下,发现效果很好.

这是我的示例.

Hi,

If this the exacy code you are using

private DataGridViewButtonColumn PDFButtonColumn; will throw exception.

I checked and see that it works fine.

Here is my example.

        public DataTable GetData()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Id", typeof(int));
            dt.Columns.Add("Name1", typeof(string));
            dt.Columns.Add("Name2", typeof(string));
            dt.Columns.Add("Name3", typeof(string));
            dt.Columns.Add("Name4", typeof(string));
            dt.Columns.Add("Name5", typeof(string));
            dt.Columns.Add("Name6", typeof(string));
            dt.Columns.Add("Name7", typeof(string));
            dt.Columns.Add("Name8", typeof(string));
            dt.Columns.Add("Name9", typeof(int));

            for (int i = 0; i < 20; i++)
            {
                dt.Rows.Add(i, i.ToString(), i.ToString(), i.ToString(), i.ToString(), i.ToString(), i.ToString(), i.ToString(), i.ToString(), i);
            }
            return dt;
        }


        private DataGridViewButtonColumn PDFButtonColumn=new DataGridViewButtonColumn();

        private void BindHistoryGrid()
        {
            dataGridView1.DataSource = GetData();

            if (dataGridView1.Columns["GetPDFFile"] != null)
                dataGridView1.Columns.Remove("GetPDFFile");

            dataGridView1.Columns[0].DisplayIndex = 0;
            dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
            dataGridView1.Columns[0].Visible = false;
            dataGridView1.Columns[1].Visible = false;
            dataGridView1.Columns.Add(PDFButtonColumn);

            dataGridView1.RowHeadersVisible = false;
            dataGridView1.ReadOnly = true;
            dataGridView1.AllowUserToAddRows = false;

            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                //if (((string)row.Cells[5].Value).Contains("Invoice"))
                if (((int)row.Cells[9].Value) > 0)
                {
                    ((DataGridViewButtonCell)(row.Cells[10])).Value = "Get Invoice";
                }
                else
                {
                    ((DataGridViewButtonCell)(row.Cells[10])).Value = "";
                }
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 10 && dataGridView1.CurrentRow.Cells[6].Value != System.DBNull.Value)
            {
                string pdfFile = "";
                int docID = 0;

                pdfFile = (string)dataGridView1.CurrentRow.Cells[5].Value + ".pdf";
                docID = (int)dataGridView1.CurrentRow.Cells[9].Value;

                if (docID > 0)
                {
                    //getPDFFile(docID, pdfFile, "pdf");
                    MessageBox.Show("Here is the pdf");
                }
                else
                {
                    MessageBox.Show("No invoice available for this item");
                }
            }
        }

        private void imgDispForm_Load(object sender, EventArgs e)
        {
            BindHistoryGrid();
        }


这篇关于文本未出现在DataGridViewButtonColumn上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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