0

Hey I have a question regarding datagridview cells, I have made made the rows that display data from the database as read only, but the thing is when I click on one of the cells an event triggers out,
I have coded for cellcontentclick, cellvalidating, currentcelldirtystatechanged, editingcontrolshowing. When I clik on the readonly cell the cell validating event triggers, so how can I make the events disable for the read-only cells.


thanxxxxxxxxx

2
Contributors
4
Replies
6
Views
6 Years
Discussion Span
Last Post by judithSampathwa
0
private void dataGridView1_CellContentClick( object sender , DataGridViewCellEventArgs e )
                {

                        if( !( (DataGridViewCell)sender ).ReadOnly )
                        { 
                            //normal event handling
                        }

                }

Edited by PierlucSS: n/a

0
private void dataGridView1_CellContentClick( object sender , DataGridViewCellEventArgs e )
                {

                        if( !( (DataGridViewCell)sender ).ReadOnly )
                        { 
                            //normal event handling
                        }

                }

hey when i add this code and click the content it gives an error saying InvalidCastException Unhandled

"Unable to cast object of type 'System.Windows.Forms.DataGridView' to type 'System.Windows.Forms.DataGridViewCell'."

my code is as below

private void dgvSubContractor_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (!((DataGridViewCell)sender).ReadOnly)
            {
                if (e.ColumnIndex == 8)
                {
                    int row = dgvSubContractor.CurrentCell.RowIndex;
                    SID = string.Format("" + dgvSubContractor.Rows[row].Cells[2].Value);
                    SName = string.Format("" + dgvSubContractor.Rows[row].Cells[3].Value);
                    PSID = string.Format("" + dgvSubContractor.Rows[row].Cells[1].Value);

                    c.setPSID(SID, SName, PSID);

                    if (!String.IsNullOrEmpty(SID))
                    {
                        if (String.IsNullOrEmpty(PSID))
                        {
                            pc.AddPSubContractDetails(c.getID().ToString(), c.getPhase(), Int32.Parse(SID), SName);

                            SName = dgvSubContractor.Rows[row].Cells[1].Value.ToString();
                            frmSubCDetails frmSub = new frmSubCDetails();
                            frmSub.ShowDialog();
                        }
                        else
                        {
                            try
                            {
                                frmSubCDetails frmSub = new frmSubCDetails();
                                frmSub.Show();
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message);
                            }
                        }
                    }
                    else
                    {
                        if (String.IsNullOrEmpty(PSID))
                        {
                            if (String.IsNullOrEmpty(SName))
                            {
                                getSubContractorDetails();
                            }
                        }
                        MessageBox.Show("insert subcontractor details first");
                    }
                }
            }
        }


thanxxxxxx
why is this happening
0

Sorry i thought it was the datagridviewcell itself sending the event, but apparently its the datagridview.

Try this

private void dataGridView1_CellContentClick( object sender , DataGridViewCellEventArgs e )
                {
                        if( !( (DataGridView)sender ).Rows[ e.RowIndex ].Cells[ e.ColumnIndex ].ReadOnly )
                        {
                                //instructions
                        }
                }

EDIT: It was happening because I wanted to treat the sender as a DataGridViewCell but it's the datagridview that raise the event.

Edited by PierlucSS: n/a

0

Sorry i thought it was the datagridviewcell itself sending the event, but apparently its the datagridview.

Try this

private void dataGridView1_CellContentClick( object sender , DataGridViewCellEventArgs e )
                {
                        if( !( (DataGridView)sender ).Rows[ e.RowIndex ].Cells[ e.ColumnIndex ].ReadOnly )
                        {
                                //instructions
                        }
                }

EDIT: It was happening because I wanted to treat the sender as a DataGridViewCell but it's the datagridview that raise the event.

hye itz working
thankxxxx

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.