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

private void dataGridView1_CellContentClick( object sender , DataGridViewCellEventArgs e )
                {

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

                }
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

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.