Hey there,
I have a interface which loads the data from the database. It loads the data to the datagridview, and text boxes. When I load the data to the datagrid view how can I make it uneditable so that the validations and other datagridview event scan not be occurred.
And also when I try to add a row to the datagridview I should only be able to edit the row that I just edited. How can I make this possible.
When I try to make the adding row editable the whole datagridview becomes editable.
How can I make only the adding row editable.
thanxxxxxxxxxx

Use the CellBeginEdit event. Not used it myself but it is a cancelable event so I think setting e.Cancel will block the edit from starting.
As with other events for the DGV, test e.ColumnIndex and e.RowIndex to only allow editing of the cells you want.

Use the CellBeginEdit event. Not used it myself but it is a cancelable event so I think setting e.Cancel will block the edit from starting.
As with other events for the DGV, test e.ColumnIndex and e.RowIndex to only allow editing of the cells you want.

how can i implement this

thnaxxx

I might recommend that you read this page as it should answer your question on how to prevent editing of your DGV.

As for the second requirement to have a single row editable (ie: the one just entered via ADD) not sure.

I strongly recommend you read through the entire page I linked above, however, as if you don't go beginning to end then some parts may not make sense :twisted: but I don't advise following any of the code examples until you've reached the end of it.

Hope this helps :) Please remember to mark solved once your issue is resolved.

I might recommend that you read this page as it should answer your question on how to prevent editing of your DGV.

That link if for a DataGrid and will not apply to the DGV.

how can i implement this

I know you have used and connected to DGV events before, which is why I am not going to post an example for you.
I checked out CellBeginEdit and setting e.Cancel to true work just as you require (i.e. it blocks the edit).
The bit that I did not explain is how to determine the added row, but since the DGV also has a UserAddedRow event this should not be too difficult.
Below I outline the required steps you need to follow.

  • Select the DGV then click on the Events button at the top of the properties window.
  • Find CellBeginEdit and double click it to create the event handler.
  • Do the same for the UserAddedRow event.
  • Add code to UserAddedRow handler to capture Index of added row. (Try e.Row.Index).
  • Add code to CellBeginEdit handler to only allow edit of added row index. (Set e.Cancel = false for added row index).
  • Test and debug.

That link if for a DataGrid and will not apply to the DGV.

My bad, must not have been paying attention when I looked it up :twisted: Liking your example though, very thorough and should work well as long as they follow the steps (and know how to follow the steps) :)

I checked this out and it is not as simple as I first thought.
For some reason the index of e.Row.Index is not the same value as the row entered after first edit is applied.
You will need to capture e.Row.Index-1 in UserAddedRow.
Also, to enable the edit row you need to test if the row IsNewRow.
I.e. also test dataGridView1.Rows[e.RowIndex].IsNewRow as one of the conditions to allow edit in CellBeginEdit.

I checked this out and it is not as simple as I first thought.
For some reason the index of e.Row.Index is not the same value as the row entered after first edit is applied.
You will need to capture e.Row.Index-1 in UserAddedRow.
Also, to enable the edit row you need to test if the row IsNewRow.
I.e. also test dataGridView1.Rows[e.RowIndex].IsNewRow as one of the conditions to allow edit in CellBeginEdit.

Just what i was about to say :p Although, if you capture the index of the added row (say as AddedRowIndex) you can encompass the NewRow check by using if(e.RowIndex < AddedRowIndex) e.Cancel = true; . That way if they are editing the added row or creating a new row the edit is allowed. Otherwise it is cancelled :)
Of course this depends on the new row being added at the end (which i'm sure is the standard behaviour).

I might recommend that you read this page as it should answer your question on how to prevent editing of your DGV.

As for the second requirement to have a single row editable (ie: the one just entered via ADD) not sure.

I strongly recommend you read through the entire page I linked above, however, as if you don't go beginning to end then some parts may not make sense :twisted: but I don't advise following any of the code examples until you've reached the end of it.

Hope this helps :) Please remember to mark solved once your issue is resolved.

hey
rather i use the datatable i am adding the data to the datagrid view as below

SqlDataAdapter da = new SqlDataAdapter(command);
            DataTable dt = new DataTable();

            da.Fill(dt);
            dgvACount = dt.Rows.Count;

            if (dgvACount == 0)
            {
            }
            else
            {
                dgvActions.Rows.Add(dgvACount);


                for (int r = 0; r < dt.Rows.Count; r++)
                {
                    dgvActions.Rows[r].Cells[0].Value = dt.Rows[r]["PActionID"].ToString();
                    dgvActions.Rows[r].Cells[1].Value = dt.Rows[r]["PDes"].ToString();
                    dgvActions.Rows[r].Cells[2].Value = dt.Rows[r]["PAssignTo"].ToString();

this is because i have added the columns from the interface and i have add the calender control from the interface so how can i achieve this solution

thanxxxx

I checked this out and it is not as simple as I first thought.
For some reason the index of e.Row.Index is not the same value as the row entered after first edit is applied.
You will need to capture e.Row.Index-1 in UserAddedRow.
Also, to enable the edit row you need to test if the row IsNewRow.
I.e. also test dataGridView1.Rows[e.RowIndex].IsNewRow as one of the conditions to allow edit in CellBeginEdit.

hey

i wrote the code as below

private static int AddedRowIndex = 0;  

 private void dgvSubContractor_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            bool i = dgvSubContractor.Rows[e.RowIndex].IsNewRow;
            if (e.RowIndex < AddedRowIndex)
            {
                e.Cancel = true;
                MessageBox.Show("e.RowIndex < AddedRowIndex");
            }
        }

        private void dgvSubContractor_UserAddedRow(object sender, DataGridViewRowEventArgs e)
        {
            AddedRowIndex = Int32.Parse(dgvSubContractor.Rows[e.Row.Index - 1].Cells[0].Value.ToString());
        }

but it dosen't work??????


thanxxxxx

hey
rather i use the datatable i am adding the data to the datagrid view as below

SqlDataAdapter da = new SqlDataAdapter(command);
            DataTable dt = new DataTable();

            da.Fill(dt);
            dgvACount = dt.Rows.Count;

            if (dgvACount == 0)
            {
            }
            else
            {
                dgvActions.Rows.Add(dgvACount);


                for (int r = 0; r < dt.Rows.Count; r++)
                {
                    dgvActions.Rows[r].Cells[0].Value = dt.Rows[r]["PActionID"].ToString();
                    dgvActions.Rows[r].Cells[1].Value = dt.Rows[r]["PDes"].ToString();
                    dgvActions.Rows[r].Cells[2].Value = dt.Rows[r]["PAssignTo"].ToString();

this is because i have added the columns from the interface and i have add the calender control from the interface so how can i achieve this solution

thanxxxx

First lets look at your code here. Rather than having an empty if statement and putting code in the else block, why dont you alter the condition: if(dgvACount != 0) or if(dgvACount > 0) .
Next, you can use a foreach loop to iterate through types which implement IEnumerable:

foreach(DataRow dr in dt.Rows)
{
    dgvActions.Rows.Add(dr["PActionID"], dr["PDes"], dr["pAssignTo"]);
}

As for your last post..the problem is that you need to store AddedRowIndex..the Index of the Row just Added.
You are storing a value from a different table :/ Why are you using dgvSubContractor.Rows[e.Row.Index - 1]?
You need to store e.Row.Index.

Also, it doesnt directly affect the problem (in fact you need to remove the line of code all together) but i felt i should point this out anyway; You are getting the value from the cell and converting it to a string in order to parse it to an int. You can cut out the conversion to string and directly convert to int:

AddedRowIndex = Convert.ToInt32(dgvSubContractor.Rows[e.Row.Index - 1].Cells[0].Value);

After a little thought about what you want, I think you do not need the UserAddedRow event.
Delete the code completely (don't forget to detach the event in the form designer).

After adding the existing rows to the DGV, just set the AddedRowIndex to the DGV row count (+/- 1 might be needed; find out by testing).
This way all new rows are left editable and all old rows are blocked from edit.

The code in your CellBeginEdit event should work as posted. (Without the MessageBox and IsNewRow lines)

To prevent a particular column from edit, just add a test on column index too.

After a little thought about what you want, I think you do not need the UserAddedRow event.
Delete the code completely (don't forget to detach the event in the form designer).

After adding the existing rows to the DGV, just set the AddedRowIndex to the DGV row count (+/- 1 might be needed; find out by testing).
This way all new rows are left editable and all old rows are blocked from edit.

The code in your CellBeginEdit event should work as posted. (Without the MessageBox and IsNewRow lines)

To prevent a particular column from edit, just add a test on column index too.

hey plaese can you explain from the beginning confused....
thanxxxx

I gave step by step instructions. How hard is it to follow them.
OK. Here it is again, with pictures!

After a little thought about what you want, I think you do not need the UserAddedRow event.
Delete the code completely (don't forget to detach the event in the form designer).

If you just remove an event method, the code that does the attach in form.designer.cs is still there. You need to remove this too. This is best done first by selecting the lighting icon in the properties window, right clicking on the UserAddedRow event and selecting Reset.

Delete this code:

private void dgvSubContractor_UserAddedRow(object sender, DataGridViewRowEventArgs e)
        {
            AddedRowIndex = Int32.Parse(dgvSubContractor.Rows[e.Row.Index - 1].Cells[0].Value.ToString());
        }

The code in your CellBeginEdit event should work as posted. (Without the MessageBox and IsNewRow lines)

private static int AddedRowIndex = 0;  

 private void dgvSubContractor_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            bool i = dgvSubContractor.Rows[e.RowIndex].IsNewRow; //<-- remove this line
            if (e.RowIndex < AddedRowIndex)
            {
                e.Cancel = true;
                MessageBox.Show("e.RowIndex < AddedRowIndex"); //<-- remove this line
            }
        }

After adding the existing rows to the DGV, just set the AddedRowIndex to the DGV row count (+/- 1 might be needed; find out by testing).

SqlDataAdapter da = new SqlDataAdapter(command);
            DataTable dt = new DataTable();

            da.Fill(dt);
            dgvACount = dt.Rows.Count;

            if (dgvACount > 0) //<-- modified like Ryshad said
            {
                AddedRowIndex = dgvACount; //<-- get first new row index (might need + 1 or - 1)

                dgvActions.Rows.Add(dgvACount);

                for (int r = 0; r < dt.Rows.Count; r++)
                {
                    dgvActions.Rows[r].Cells[0].Value = dt.Rows[r]["PActionID"].ToString();
                    dgvActions.Rows[r].Cells[1].Value = dt.Rows[r]["PDes"].ToString();
                    dgvActions.Rows[r].Cells[2].Value = dt.Rows[r]["PAssignTo"].ToString();

To prevent a particular column from edit, just add a test on column index too.

I'll leave that to you to figure out as homework.;)

commented: couldnt possibly be clearer +1
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.