Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > .NET 3.5 and Visual Studio. 2008 > Visual Studio 2008
|
Visual Studio 2008 For discussing Visual Studio 2008. Please post code questions about a specific language (C#, VB, ASP.NET, etc) in the correct language forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Studio 2008 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 10th, 2009, 05:46 AM
Authorized User
 
Join Date: Jul 2007
Posts: 29
Thanks: 2
Thanked 0 Times in 0 Posts
Default Help with DataGridView(windows app) boolean column.

Hi,
I have a DGV with a data table as datasource.
One of the columns in the data table is a boolean column and is shown as a checkbox column at runtime.
I am trying to change the state of the checkbox depending on a condition.
When the checkbox is unchecked and the user clicks on it, a messagebox pops up asking if the user wants to mark it. This is a yesnocancel messagebox. The checkbox should be checked for both yes and no conditions and should remain unchecked for cancel.
If the checkbox is checked and the user clicks it then a yesno messagebox pop's up asking to uncheck it. It should get unchecked for 'yes' and remain checked for 'No'.
Here is the code that I am trying out, which does not work:

Code:
 

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1)
            {
                if (dataGridView1.CurrentCell == dataGridView1.Rows[e.RowIndex].Cells[0])
                {
                    if ((bool)dataGridView1.Rows[e.RowIndex].Cells[0].Value == false)
                    {
                        DialogResult dr = MessageBox.Show("Delete the value?", "Delete", MessageBoxButtons.YesNoCancel);
                        
                        dataGridView1.Rows[e.RowIndex].Cells[0].Value = (dr == DialogResult.Yes || dr == DialogResult.No) ? true : false;

                    }
                    else if ((bool)dataGridView1.CurrentCell.Value == true)
                    {

                        DialogResult dr = MessageBox.Show("Unmark the value?", "Delete", MessageBoxButtons.YesNo);
                        
                        dataGridView1.Rows[e.RowIndex].Cells[0].Value = (dr == DialogResult.Yes) ? false :true ;

                    }
                    dataGridView1.ClearSelection();
                }

            }
        }


I have been trying to make it work for quite some time now, with no results.
Any help would be greatly appreciated.
 
Old August 10th, 2009, 08:15 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Hello.. Would be nice to know which part is not working...
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
 
Old August 10th, 2009, 08:46 AM
Authorized User
 
Join Date: Jul 2007
Posts: 29
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Basically the above code is what I have in my app and that does not work.
I am not able to figure out why the cell value is not reflected even when its explicitly changed in the code.
 
Old August 10th, 2009, 08:57 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Probably because value is not expecting a boolean type. If my mind doesn't trick me, all the value are string type... or numeric ones.
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
 
Old August 11th, 2009, 08:59 AM
Authorized User
 
Join Date: Jul 2007
Posts: 29
Thanks: 2
Thanked 0 Times in 0 Posts
Default DGV shows the first cell of first row as current selction.

When the DGV loads, it's showing the first cell of first row as the current selection.
I removed the code from CellClick and now have this code in the BeginEdit method
This works fine, but does not show the message box for the first cell for first row.

Any ideas!.

Code:
private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            int columnIndex = e.ColumnIndex;
            int rowIndex = e.RowIndex;
            //if (rowIndex != dataGridView1.SelectedRows[0].Index)
            //    return;
            if (!(firstRun))
            {
                if (e.RowIndex != -1)
                {
                    //dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
                    //if (e.ColumnIndex == 0)
                    if (dataGridView1.CurrentCell == dataGridView1.Rows[e.RowIndex].Cells[0])
                    {
                        //dataGridView1.BeginEdit(false);
                        if ((bool)dataGridView1.CurrentCell.Value == true)
                        {
                            DialogResult dr = MessageBox.Show("UnDelete the value " + dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString() + "?", "Delete", MessageBoxButtons.YesNo);

                            if (dr == DialogResult.Yes)
                            {
                                dataGridView1.Rows[e.RowIndex].Cells[1].Value = "";
                                e.Cancel = true;
                                dataGridView1.ClearSelection();
                            }
                            //e.Cancel = (dr == DialogResult.Yes);


                            dataGridView1.CurrentCell.Value = (dr == DialogResult.No);
                            e.Cancel = true;
                            //dataGridView1.Rows[e.RowIndex].Cells[1].Value = "";
                            //dataGridView1.ClearSelection();

                        }
                        else if ((bool)dataGridView1.CurrentCell.Value == false)
                        {
                            DialogResult dr = MessageBox.Show("Delete the value " + dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString() + "?", "Delete", MessageBoxButtons.YesNoCancel);
                            //e.Cancel = (dr == DialogResult.Cancel);
                            // e.Cancel = true;

                            if (dr == DialogResult.Yes)
                            {
                                markedColumns.Add(dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString() + "-" + dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString());

                                frmListValues objFrmListValues = new frmListValues(dtTable, dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString(), "customer", markedColumns);
                                objFrmListValues.ShowDialog();

                                string retValue = objFrmListValues.selectedValue;

                                if (retValue != null)
                                {
                                    // blnIsEdited = true;
                                    dataGridView1.Rows[e.RowIndex].Cells[1].Value = retValue;
                                    //dataGridView1.Rows[e.RowIndex].Cells[0].Value = true;
                                    e.Cancel = true;

                                    dataGridView1.ClearSelection();
                                }
                                else
                                {
                                    markedColumns.Remove(dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString() + "-" + dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString());
                                    //dataGridView1.Rows[e.RowIndex].Cells[0].Value = null;
                                    e.Cancel = true;

                                    dataGridView1.ClearSelection();
                                }
                            }
                            else if (dr == DialogResult.No)
                            {
                                //MessageBox.Show("Value will be cleared!");
                                markedColumns.Add(dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString());
                                //blnIsEdited = true;
                                //dataGridView1.Rows[e.RowIndex].Cells[0].Value = true;
                                e.Cancel = true;
                                dataGridView1.ClearSelection();
                            }

                            dataGridView1.CurrentCell.Value = (dr == DialogResult.Yes || dr == DialogResult.No);
                            e.Cancel = true;
                            dataGridView1.ClearSelection();
                        }
                    }
                }
            }
            else if (rowIndex == 0 && columnIndex == 0)
            {
                firstRun = false;
            }

        }





Similar Threads
Thread Thread Starter Forum Replies Last Post
Starting a console App from a vb.net windows app RobCarter Pro Visual Basic 2005 4 June 24th, 2009 09:20 AM
not showing column in datagridview jomet .NET Framework 2.0 6 March 15th, 2008 04:02 AM
DataGridView - allow column resize snufse .NET Framework 2.0 0 January 31st, 2008 10:33 AM
DataGridView column value snufse .NET Framework 2.0 3 January 30th, 2008 10:06 AM
Accessing Windows service from a windows app sajid08 C# 1 October 6th, 2006 10:25 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.