I have modified the samle code at Drag and Drop GridView Rows with jQuery in ASP.Net.
I have added a new field ItemId which is the unique id and to its BoundField I have given a class ItemId.
And then I am verifying whether the row is present or not, and if it is then a alert message is displayed
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.8.0.js" type="text/javascript"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.22/jquery-ui.js"></script>
<link rel="Stylesheet" href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.10/themes/redmond/jquery-ui.css" />
<script type="text/javascript">
$(function () {
$(".drag_drop_grid").sortable({
items: 'tr:not(tr:first-child)',
cursor: 'crosshair',
connectWith: '.drag_drop_grid',
axis: 'y',
dropOnEmpty: true,
change: function (e, ui) {
if ($(this).attr("id") != ui.item.closest("table").attr("id")) {
var isExists = false;
$(this).find(".ItemId").each(function () {
if (!isExists) {
isExists = ui.item.find(".ItemId").html() == $(this).html();
}
});
if (isExists) {
$(".drag_drop_grid").sortable("cancel");
alert("Duplicate Item!");
}
}
}
});
});
</script>
<style type="text/css">
.GridSrc td
{
background-color: #A1DCF2;
color: black;
font-size: 10pt;
font-family: Arial;
line-height: 200%;
cursor: pointer;
width: 100px;
}
.GridSrc th
{
background-color: #3AC0F2;
color: White;
font-family: Arial;
font-size: 10pt;
line-height: 200%;
width: 100px;
}
.GridDest td
{
background-color: #eee !important;
color: black;
font-family: Arial;
font-size: 10pt;
line-height: 200%;
cursor: pointer;
width: 100px;
}
.GridDest th
{
background-color: #6C6C6C !important;
color: White;
font-family: Arial;
font-size: 10pt;
line-height: 200%;
width: 100px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="gvSource" runat="server" CssClass="drag_drop_grid GridSrc" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="ItemId" HeaderText="Id" ItemStyle-CssClass="ItemId" />
<asp:BoundField DataField="Item" HeaderText="Item" />
<asp:BoundField DataField="Price" HeaderText="Price" />
</Columns>
</asp:GridView>
<hr />
<asp:GridView ID="gvDest" runat="server" CssClass="drag_drop_grid GridDest" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="ItemId" HeaderText="Id" ItemStyle-CssClass="ItemId" />
<asp:BoundField DataField="Item" HeaderText="Item" />
<asp:BoundField DataField="Price" HeaderText="Price" />
</Columns>
</asp:GridView>
</form>
</body>
</html>
Namespaces
using System.Data;
Code
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("ItemId"), new DataColumn("Item"), new DataColumn("Price") });
dt.Rows.Add(1, "Shirt", 450);
dt.Rows.Add(2, "Jeans", 3200);
dt.Rows.Add(3, "Trousers", 1900);
dt.Rows.Add(4, "Tie", 185);
dt.Rows.Add(5, "Cap", 100);
dt.Rows.Add(6, "Hat", 120);
dt.Rows.Add(7, "Scarf", 290);
dt.Rows.Add(8, "Belt", 150);
gvSource.UseAccessibleHeader = true;
gvSource.DataSource = dt;
gvSource.DataBind();
dt.Rows.Clear();
dt.Rows.Add(1, "Shirt", 450);
dt.Rows.Add(2, "Jeans", 3200);
dt.Rows.Add(3, "Trousers", 1900);
gvDest.DataSource = dt;
gvDest.DataBind();
}
}