Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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
 
            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 8-Nov-12 3:25am
Edited 8-Nov-12 3:29am
v3
Comments
adriancs at 8-Nov-12 9:30am
   
data.GetType().ToString()
what do you get?
adriancs at 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 at 8-Nov-12 10:48am
   
First of all, did you really make an instance of grid view static? And why?
--SA
vivek886 at 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]
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Comments
vivek886 at 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 at 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
Marcus Kramer at 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 at 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
Marcus Kramer at 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)
Sergey Alexandrovich Kryukov at 8-Nov-12 17:07pm
   
Thank you, Marcus.
No, you did not imply that; I'm just trying to be more conservative. Using static instance of it is certainly a bad idea.
--SA
vivek886 at 9-Nov-12 13:09pm
   
not a bad idea, everyone has different way of designing a system, where needs are particular, and never have to work hypothetically.
Sergey Alexandrovich Kryukov at 9-Nov-12 13:20pm
   
You are right in principle, but in this particular case -- do you think there is a good reason to use a static control member? I don't think so; I think this is apparently the bad practice. I could agree with you on this particular case only if you provided some compelling reason to use such static object, but I doubt you would be able to do so.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

 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 = "";
                }
                
            }
        }
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 6,656
1 Sergey Alexandrovich Kryukov 6,490
2 CPallini 5,310
3 George Jonsson 3,584
4 Gihan Liyanage 2,650


Advertise | Privacy | Mobile
Web04 | 2.8.140921.1 | Last Updated 9 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100