Click here to Skip to main content
15,881,875 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have inserted values in grid view and i dont want duplicate values in that gridview how it will be?

means if i again insert same values it should show error.

below is my insert code for gridview

C#
rejfields s1 = new rejfields();



s1.Application = ddplAR.SelectedItem.ToString();
s1.Section = ddplRS.SelectedItem.ToString();
data.Add(s1);
ViewState["_data"] = data;

GridView2.DataSource = data;
GridView2.DataBind();
Posted
Updated 8-Nov-12 3:29am
v3
Comments
adriancs 8-Nov-12 9:30am    
data.GetType().ToString()
what do you get?
adriancs 8-Nov-12 9:50am    
check if the values exist in data or not.
if yes, throw error
if no, add it.
Sergey Alexandrovich Kryukov 8-Nov-12 10:48am    
First of all, did you really make an instance of grid view static? And why?
--SA
vivek886 8-Nov-12 11:39am    
I simplify my question.

From my webform i send values to Gridview.
then i send the values from grid to database.

When i send my values to gridview, i want if i have already added particular record to gridview then again it should not be added and remains unique in the gridview.

data.GetType().ToString() returns
System.Collections.Generic.List`1[rejfields]

Please see my comment: there is no need to make control instances static. Do you really understand what is static? Perhaps you did not do it, but just use wrong or ambiguous terminology.

The answer depends on what you bind your grid view with. If this is a relational database table, think about having non-duplicated data in the table.

If situation is more complex, you might want to have an intermediate data source which guarantees uniqueness, such as some dictionary of records, System.Collections.Generic.Dictionary and bind you grid view with the instance of it — dictionary keys are unique.

—SA
 
Share this answer
 
Comments
vivek886 8-Nov-12 11:42am    
i do understand what is static. Grid always have same record fixed, no connectivity to database. grid is binded with fixed values or record only. but if sometimes i need to add record once in a while it should check for duplicated record entry in gridview @ binding time.
vivek886 8-Nov-12 11:46am    
the system we designed needs this kind of gridview @ someplace compulsary for administrative purpose only.

thanks Sergery for making me think twice again, and again simplify my question
fjdiewornncalwe 8-Nov-12 14:12pm    
The DataGrid which holds the data that can be made static in certain situations, but the Control should NEVER be made static.
Sergey Alexandrovich Kryukov 8-Nov-12 14:32pm    
Technically speaking, it can be done static and work, but I totally agree if you would NEVER recommend it.
--SA
fjdiewornncalwe 8-Nov-12 14:40pm    
I hope I didn't imply that it wouldn't work, because it does and I've seen fixed a few projects where people did that. What I meant to reiterate and support from your answer is that it really is a bad practice because it is akin to using a UI control as a Business object. Cheers. (+5, BTW)
C#
rejfields s1 = new rejfields();

           if(ddplRS.SelectedValue == "0")
           {
               lblDataMsg.Text = "Please select Reject Section.";
           }
           else if (GridView2.Rows.Count.Equals(0))
           {
               s1.Section = ddplRS.SelectedItem.ToString();
               s1.Description = ddplRS0.SelectedItem.ToString();
               data.Add(s1);
               ViewState["_data"] = data;

               GridView2.DataSource = data;
               GridView2.DataBind();
               lblDataMsg.Text = "";


           }
           else
           {
               bool found = false;
               for (int i = 0; i < GridView2.Rows.Count; i++)
               {
                       if (GridView2.Rows[i].Cells[0].Text == ddplRS.SelectedItem.Text)
                       {
                           lblDataMsg.Text = String.Format("Section {0} already added.", GridView2.Rows[i].Cells[0].Text);
                           found = true;
                       }
               }
               if (!found)
               {
                   s1.Section = ddplRS.SelectedItem.ToString();
                   s1.Description = ddplRS0.SelectedItem.ToString();
                   data.Add(s1);
                   ViewState["_data"] = data;

                   GridView2.DataSource = data;
                   GridView2.DataBind();
                   lblDataMsg.Text = "";
               }

           }
       }
 
Share this answer
 

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