C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




How to perform deletion operation in GridView control when checkBox Buttons are clicked


Posted Date: 05 Jul 2007      Total Responses: 2

Posted By: Manjula       Member Level: Bronze     Points: 2


I am using .net2005 version



Responses

Author: veeresh    05 Jul 2007Member Level: DiamondRating:     Points: 2
<asp:GridView ID="GridView1" datakeyname="Employeeid" runat="server" DataSourceID="Sqldatasource1">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="CheckAll" onclick="return check_uncheck (this );" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="EmpID" Visible="false"
Text='<%# DataBinder.Eval (Container.DataItem, "employeeID") %>'
runat="server" />
<asp:CheckBox ID="deleteRec" onclick="return check_uncheck (this );"
runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" OnClientClick="return confirmMsg(this.form)"
Text="Button" OnClick="Button1_Click" />
<asp:SqlDataSource ID="Sqldatasource1" runat="server"
SelectCommand="Select employeeid,firstname,lastname from Employees"
ConnectionString="Server=localhost;uid=sa;password=;database=Northwind">
</asp:SqlDataSource>

protected void Button1_Click(object sender, EventArgs e)
{
string gvIDs = "";
bool chkBox = false;
//'Navigate through each row in the GridView for checkbox items
foreach (GridViewRow gv in GridView1.Rows)
{
CheckBox deleteChkBxItem = (CheckBox)gv.FindControl("deleteRec");
if (deleteChkBxItem.Checked)
{
chkBox = true;
// Concatenate GridView items with comma for SQL Delete
gvIDs += ((Label)gv.FindControl("EmpID")).Text.ToString() + ",";
}
}

SqlConnection cn = new SqlConnection(Sqldatasource1.ConnectionString);
if (chkBox)
{
// Execute SQL Query only if checkboxes are checked to avoid any error with initial null string
try
{
string deleteSQL = "DELETE from employees WHERE employeeid IN (" +
gvIDs.Substring(0, gvIDs.LastIndexOf(",")) + ")";
SqlCommand cmd = new SqlCommand(deleteSQL, cn);
cn.Open();
cmd.ExecuteNonQuery();
GridView1.DataBind();
}
catch (SqlException err)
{
Response.Write(err.Message.ToString());
}
finally
{
cn.Close();
}

}
}

JavaScript
function check_uncheck(Val)
{
var ValChecked = Val.checked;
var ValId = Val.id;
var frm = document.forms[0];
// Loop through all elements
for (i = 0; i < frm.length; i++)
{
// Look for Header Template's Checkbox
//As we have not other control other than checkbox we just check following statement
if (this != null)
{
if (ValId.indexOf('CheckAll') != - 1)
{
// Check if main checkbox is checked,
// then select or deselect datagrid checkboxes
if (ValChecked)
frm.elements[i].checked = true;
else
frm.elements[i].checked = false;
}
else if (ValId.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;
}
} // if
} // for
} // function

function confirmMsg(frm)
{
// loop through all elements
for (i = 0; i < frm.length; i++)
{
// Look for our checkboxes only
if (frm.elements[i].name.indexOf("deleteRec") != - 1)
{
// If any are checked then confirm alert, otherwise nothing happens
if (frm.elements[i].checked)
return confirm('Are you sure you want to delete your selection(s)?')
}
}
}


Author: karthikeyanb    05 Jul 2007Member Level: GoldRating:     Points: 2
Hai I am karthi. I too have this problem. Pls mail me the answer to bk_karthi@rediffmail.com


Post Reply
You must Sign In to post a response.
Next : how to convert numbers into words in crystal report
Previous : Please help me about the question on heart of .net framework
Return to Discussion Forum
Post New Message
Category: .NET

Related Messages



dotNet Slackers   BizTalk Adaptors    Web Design

conference calls

Contact Us    Privacy Policy    Terms Of Use