Click here to Skip to main content
15,914,419 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a DataGridView with 3 rows.

I am adding a fourth row programmatically.

Is there any way to prevent the user from selecting the three rows after I add the fourth row.

Thanks a ton everyone
:)
Posted

1 solution

Hi
I am not cleared about our question very much but the logic is something like:
C#
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
   {
       //If Rows > 3 then it will not display selected row (from 1 to 3 rows)
       if (GridView1.Rows.Count > 3)
       {
           if (e.NewSelectedIndex > 2)
           {
               //If Rows > 3 and SelectedIndex > 2 (it started from 0) then it will display selected row (from 4 to n..)
               GridView1.Rows[e.NewSelectedIndex].CssClass = "AllowSelection";
           }
           else
           {
               //If Rows > 3 and SelectedIndex < 2 (it started from 0) then it will not display selected row (0 to 2)
               GridView1.Rows[e.NewSelectedIndex].CssClass = "DisallowedSelection";
           }
       }
       else
       {
           //If Rows < 3 then it will display selected row (0 to 2)
           GridView1.Rows[e.NewSelectedIndex].CssClass = "AllowSelection";
       }
   }


Please do let me know, if you have any doubt.

Please provide Vote if this would be helpful to you.

Thanks,
Imdadhusen
 
Share this answer
 
Comments
RebornDeveloper 17-Sep-10 4:36am    
Hi There

I understand that this would be the viable option in case my application was a web/ asp/net based app.

Unfortunately http://s.codeproject.com/script/Forums/Images/smiley_cry.gif that is not so.

I am working with the Winforms version of the datagridview

Thanks so much anyway

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900