如何在asp.net中比较静态gridview中的值 [英] How to compare Values from static gridview in asp.net

查看:52
本文介绍了如何在asp.net中比较静态gridview中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已在网格视图中插入值,并且我不想在该网格视图中重复值,结果将如何?

表示如果我再次插入相同的值,它应该显示错误.

以下是我的gridview插入代码

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();

推荐答案

请参阅我的评论:无需使控件实例静态.你真的了解什么是静态的吗?也许您没有这样做,只是使用了错误或模棱两可的术语.

答案取决于您绑定网格视图所用的对象.如果这是一个关系数据库表,请考虑在表中具有非重复数据.

如果情况更为复杂,则可能需要一个保证唯一性的中间数据源,例如一些记录字典,System.Collections.Generic.Dictionary,并用它的实例绑定到网格视图-字典键是唯一的.

-SA
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


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

           }
       }


这篇关于如何在asp.net中比较静态gridview中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆