Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have a grid view like..
C#
sno     Name   ID

 1      AAA     1
 2      BBB     2
 3      CCC     3
 4      DDD     6

If you observe above Gridview..In this gridview missing 4 and 5 ID numbers..Now i want to shows an alert message like 4,5 ID are missing...

How can i do this?

Please help me
Posted
Updated 2-Sep-14 21:01pm
v3
Comments
kbrandwijk 3-Sep-14 7:50am    
I assume the data is actually coming from somewhere. In that case, I think this is the type of check that you want to do on the underlying datasource, and not on the grid itself.

  1. Loop through the Rows.
  2. Store Ids in a List.
  3. Loop through the List and compare current and next Id. If difference is greater than 1, then find missing Id and store in another List.
  4. At last show the those Ids.
 
Share this answer
 
1. Fetch the First row id and last row id.
2. create Enumerable range

C#
IEnumerable<int> nos= Enumerable.Range(firstrowid, lastrowid);


3. use linq to get missing nos.

C#
var missingid = from a in nos
              where !(from b in gvRows.AsEnumerable()
                        select b["id"])
                        .Contains(a)
              select a;
 
Share this answer
 
v3
Comments
George Jonsson 3-Sep-14 5:57am    
1. Also get the ID for the first row. If the first ID is much larger than 1 it will less efficient to start with 1.
2. b.id should be b["ID"]
Gupta Poonam 3-Sep-14 6:41am    
Thanks George..
Yup you are right there might be a case where first row is much larger than 1.
Will improve my answer.
This question often comes up, and sadly, the most common (and most portable) answer is to create a temporary table to hold the IDs that should be there, and do a left join. The syntax is pretty similar between MySQL and SQL Server. The only real difference is the temporary tables syntax.

SQL
declare @id int
declare @maxid int

set @id = 1
select @maxid = max(id) from tbl

create table #IDSeq
(
    id int
)

while @id < @maxid --whatever you max is
begin
    insert into #IDSeq values(@id)

    set @id = @id + 1
end

select
    s.id
from
    #idseq s
    left join tbl t on
        s.id = t.id
 where t.id is null

 drop table #IDSeq
 
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