views:

110

answers:

3

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, then I loop through the rows and based on the value of a certain column I set the text of the cell in the button column. When I step through the code I can see the ColumnIndex of the button column is 10. 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";                }
        }
    }
A: 

i too have the same problem. have u find any solution yet???

sudhir
A: 

I called my bindGrid() method from the two place one after the InitializeComponent() in form's constructor as well as from form1_load(). it works for me.

hope this will also helps you.

sudhir
A: 

I didn't get any replies here so I posted on another forum. I eventually got an answer of sorts, but the whole thing is still pretty vague. The answer I got stated that in order to preserve resources, the grid doesn't always refresh itself. An example is if you have a form with a tab control that has 2 tabs, place a grid on the 1st tab and set column properties after binding in Form Load. This will work. However, when you place the grid on the 2nd tab, using the same binding won't work:

http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/99ab9fbf-9eaa-4eef-86b8-8f4e49fa81c5

I still haven't found out how or when it decides to preserve resources, if there's a way to bypass this behaviour, if this behaviour is documented anywhere etc. If anyone can throw more light on it I'm all ears.

Ciaran Bruen