again i come up with new tricks of GridView in that you can Delete / Update / Move
seleted records from Gridview, take a Example of Gmail inbox it shows total number of mail on your "Inbox" in that have a option like Delete , move to another folder , Delete all , and so on, same feature you can add in your Gridview
look at the Below Example hope u like it .
Steps
1.using javaScript we can select Or deselect the record from GridView
<asp:GridView ID="GridView1" datakeyname="Mailid" runat="server"
DataSourceID="Sqldatasource1">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="CheckAll" runat="server" onclick="return check(this);"/>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="MailID" runat="Server"
Text='<%# DataBinder.Eval (Container.DataItem, "MailID") %>'
/>
<asp:CheckBox ID="DeleteRec" runat="server" onclick="return check(this);"/>
</ItemTemplate>
</asp:TemplateField>
</asp:GridView>
<asp:SqlDataSource ID="Sqldatasource1" runat="server"
SelectCommand="Select MailId,MailBody,Date,To,From from MailInbox"
ConnectionString="Server=MailEngine;uid=sa;password=;database=Inbox">
</asp:SqlDataSource>
2. we have Button Which act as a Delete /Move / Upadate Operation.
<asp:Button ID="Button1" OnClientClick="return confirmMsg(this.form)"
Text="Move" OnClick="Button1_Click" runat="server" />
3. Javascript code which can check the if any of Coloum get Select or Deselected.
function check(Val)
{
var MailChecked = Val.checked;
var MailId = Val.id;
var frm = document.forms[0];
// Loop through all Records in GridView
for (i = 0; i < frm.length; i++)
{
// Also Check if the Heder Template Option get Checked or Not
if (this != null)
{
if (MailId.indexOf('CheckAll') != - 1)
{
// Now check if Header template checkbox is Checked so it will Delete/Move all the
Records ,
// Then select or deselect datagrid checkboxes
if (MailChecked)
frm.elements[i].checked = true;
else
frm.elements[i].checked = false;
}
else if (MailId.indexOf('DeleteRec') != - 1)
{
// Check if any of the checkboxes are not checked,
and then uncheck top select all checkbox
if (frm.elements[i].checked == false)
frm.elements[1].checked = false;
}
}
}
}
4.Now we can Configure Move/Delete Configration Message .
function confirmMsg(frm)
{
// Now Again gone through the All the Gridview Elements
for (i = 0; i < frm.length; i++)
{
// Check for checkboxes only
if (frm.elements[i].name.indexOf("DeleteRec") != - 1)
{
// Show Alert message to End user to Really want to Delete / Move .
if (frm.elements[i].checked)
return confirm('Do you really want to Move/Delete Seleted Item')
}
}
}
5. on CodeBehind Button1_Click Event Perform the Action.
protected void Button1_Click(object sender, EventArgs e)
{
bool chkBox = false;
string GvID = "";
//Check each row in GridView for Select ot not.
foreach (GridViewRow gv in GridView1.Rows)
{
CheckBox MoveSelectedCheck = (CheckBox)gv.FindControl("DeleteRec");
if (MoveSelectedCheck.Checked)
{
chkBox = true;
// add the Seleted item with (;) so it can delete that or Move that
GvID += ((Label)gv.FindControl("MailID")).Text.ToString() + ",";
}
}
SqlConnection conn = // Define Connection String.
if (chkBox)
{
try
{
// Suppose Folder name or Lable name is (OldMail)
string MoveRec = "Update MailBox set Label ='+ OldMail +' where MailID IN (" +
GvID.Substring(0, GvID.LastIndexOf(",")) + ")";
SqlCommand cmd = new SqlCommand(MoveRec, conn);
conn.Open();
cmd.ExecuteNonQuery();
GridView1.DataBind();
}
catch (SqlException err)
{
Response.Write("Error at :-"+err);
}
finally
{
conn.Close();
}
}
}
Like this you can Perform Deletion or Updataion or Moving Oepration ,
if you are Deleting Records then its very easy you can use Delete query
if it is Updation use Update query .But in the case of Moving just like (Gmail inbox ) you have one option you can set the Status of that Seleted Mail as (Name of Folder) just like (Label part in Gmail )
and when you get click on that lable do code such that, Check if the any records from Gridview are Marked as (Lable name ) and you query according to same.
plz comment on it , if u have any query .......
Happy coding,
No comments:
Post a Comment