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 !




Insert using a checkboxes in a gridview


Posted Date: 28 Aug 2008      Total Responses: 4

Posted By: Chieu       Member Level: Bronze     Points: 1


Hi,

I am new in asp.net and need some guidance. I have a gridview and have added a check box besides each row and a insert button at the bottom.

If the insert button is clicked then it will check all the checkboxes that are check and insert this into sql database.

I am using vb as the code behind.

Thanks.




Responses

Author: Raju.M    28 Aug 2008Member Level: GoldRating:     Points: 3
this is only a rough figure of code coz i have no visual studio with me now.
for(int i=0;i<gridview.rows.count;i++)
{
//if checkbox in 0th row
checkbox cbx=(Checkbox)gridview.rows[0].cell[i].findControl("CheckBoxID");
if(cbx!=null)
{
//do ur insert command here like
string cmd=string format("INSERT INTO database(a,b) VALUES('{0},'{1})",gridView.rows[i].cells[1].text,gridView.rows[i].cells[2].text,;
insert(cmd);
}
}



Author: Raju.M    28 Aug 2008Member Level: GoldRating:     Points: 3
sorry i miss one thing

for(int i=0;i<gridview.rows.count;i++)
{
//if checkbox in 0th row
checkbox cbx=(Checkbox)gridview.rows[0].cell[i].findControl("CheckBoxID");
if(cbx!=null)
{
if(cbx.checked)
{
//do ur insert command here like
string cmd=string format("INSERT INTO database(a,b) VALUES('{0},'{1})",gridView.rows[i].cells[1].text,gridView.rows[i].cells[2].text,;
insert(cmd);
}
}



Author: Appukuttan    29 Aug 2008Member Level: DiamondRating:     Points: 3
Hi..

<asp:GridView ID="gvLeaveApproval" runat="server" AutoGenerateColumns="False" BorderStyle="None"
BorderWidth="0px" CellPadding="3" DataKeyNames="Id" EnableTheming="true" Width="100%"
SkinID="gridviewSkin" OnRowDataBound="gvLeaveApproval_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Id" Visible="false">
<ItemTemplate>
<asp:Label ID="lblId" runat="server" Text='<%# Bind("Id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="StartDate">
<HeaderStyle HorizontalAlign="center" />
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:Label ID="lblStartDate" runat="server" Text='<%# Bind("StartDate", "{0:dd/MM/yyyy}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EndDate">
<HeaderStyle HorizontalAlign="center" />
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:Label ID="lblEndDate" runat="server" Text='<%# Bind("EndDate", "{0:dd/MM/yyyy}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="No Of Days">
<HeaderStyle HorizontalAlign="center" />
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:Label ID="lblNoOfDays" runat="server" Text='<%# Bind("NumberOfdays") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Nature Of Leave">
<HeaderStyle HorizontalAlign="center" />
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:Label ID="lblNatureOfLeave" runat="server" Text='<%# Bind("NatureOfLeave") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Reason">
<HeaderStyle HorizontalAlign="center" />
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:Label ID="lblReason" runat="server" Text='<%# Bind("Reason") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Approved" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center">
<%-- <HeaderTemplate>
<input id="chkAll" onclick="javascript:SelectAllCheckboxes(this);" runat="server" type="checkbox" />
</HeaderTemplate> OnClick="javascript:return select_deselectAll(this.checked, this.id);"--%>
<HeaderTemplate>
<asp:CheckBox ID="chkCheckAll" runat="server" AutoPostBack="true" EnableViewState="true" OnCheckedChanged="chkCheckAll_CheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" AutoPostBack="true" runat="server" OnCheckedChanged="CheckBox1_CheckedChanged1"
Checked='<%# Bind("IsApproval") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:CheckBox ID="chkApproval" runat="server" Checked='<%# Bind("IsApproval") %>'
Enabled="false" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<table align="left" class="boxbg">
<tr>
<td>
NO LEAVE DETAILS AVALIABLE FOR THIS EMPLOYEE
</td>
</tr>
</table>
</EmptyDataTemplate>
</asp:GridView>


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SourceEdge.BCL;
using Tetragon.Spruce.Model;
using System.Collections.Generic;

namespace VetCareWeb
{
public partial class Leaveapplication_approval : System.Web.UI.Page
{
#region Global
string Script;
#endregion

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
tblApproval.Visible = false;
DDLGetUnderEmployeesName();
}
if (ViewState["Count"] != null && ViewState["GridCount"] != null)
{
if (ViewState["Count"] == ViewState["GridCount"])
{
CheckBox chkAll = (CheckBox)gvLeaveApproval.HeaderRow.Cells[6].FindControl("chkCheckAll");
if (chkAll != null)
{
chkAll.Checked = true;
}
}
}

}

private void DDLGetUnderEmployeesName()
{

ddlEmployeesName.Items.Clear();
ddlEmployeesName.Items.Add(new ListItem("----Select Employee----", "none"));
List<EmployeeEntity> EmpLst = VetCareUtilities.getUnderEmployee();
if (EmpLst.Count > 0)
{
foreach (EmployeeEntity EmpObj in EmpLst)
{
ddlEmployeesName.Items.Add(new ListItem(EmpObj.Name.ToString(), EmpObj.Id.ToString()));
}
}
}

private void fillLeave(string strEmployeeId)
{
List<LeaveDetailEntity> LeaveLst = Database.SelectFrom(LeaveDetailEntity.Table)
.Where(LeaveDetailEntity.Table.EmployeeId_Id.In(strEmployeeId))
.OrderBy(LeaveDetailEntity.Table.StartDate)
.LoadList<LeaveDetailEntity>();

if (LeaveLst.Count > 0)
{
tblApproval.Visible = true;
gvLeaveApproval.DataSource = LeaveLst;
gvLeaveApproval.DataBind();
}
else
{
tblApproval.Visible = false;
lblMessage.Text = "";
Script = VetCareUtilities.GetAlert(ddlEmployeesName.SelectedItem.Text + " is not applied leave for any date");
ClientScript.RegisterStartupScript(typeof(Page), "leavenot", Script);
}

}

protected void ddlEmployeesName_SelectedIndexChanged(object sender, EventArgs e)
{
fillLeave(ddlEmployeesName.SelectedValue.ToString());
GetCheckallValue();
}

private void GetCheckallValue()
{
string sValue = string.Empty;
CheckBox chkAll = new CheckBox();
foreach (GridViewRow gvr in gvLeaveApproval.Rows)
{
CheckBox chkApprove = (CheckBox)gvr.FindControl("chkApproval");
if (chkApprove != null)
{
chkAll = (CheckBox)gvLeaveApproval.HeaderRow.Cells[6].FindControl("chkCheckAll");
if (sValue == "")
{
sValue = chkApprove.Checked.ToString();
}
else
{
sValue = sValue + "," + chkApprove.Checked.ToString();
}
}
}
if (sValue.Contains("False"))
{
if (chkAll != null)
{
chkAll.Checked = false;
}
}
else
{
if (chkAll != null)
{
chkAll.Checked = true;
}
}
}

protected void CheckBox1_CheckedChanged1(object sender, EventArgs e)
{

CheckBox checkbox = (CheckBox)sender;
GridViewRow row = (GridViewRow)checkbox.NamingContainer;
GridView grid = (GridView)row.NamingContainer;
string RowId = (grid.DataKeys[row.RowIndex].Value.ToString());

LeaveDetailEntity getLeaveObj = Database.SelectFrom(LeaveDetailEntity.Table)
.Where(LeaveDetailEntity.Table.Id == RowId.ToString())
.LoadEntity<LeaveDetailEntity>();

if (getLeaveObj != null)
{

if (checkbox.Checked == true)
{
getLeaveObj.IsApproval = true;
Script = VetCareUtilities.GetAlert("Approval OK");
Page.ClientScript.RegisterStartupScript(typeof(Page), "approvalok ", Script);
getLeaveObj.Save();
fillLeave(ddlEmployeesName.SelectedValue.ToString());

}
else if (checkbox.Checked == false)
{
getLeaveObj.IsApproval = false;
Script = VetCareUtilities.GetAlert("Approval Cancel");
Page.ClientScript.RegisterStartupScript(typeof(Page), "approvalcancel ", Script);
getLeaveObj.Save();
fillLeave(ddlEmployeesName.SelectedValue.ToString());
}
}
GetCheckallValue();
}


protected void chkCheckAll_CheckedChanged(object sender, EventArgs e)
{
int iCount = 0;
CheckBox chkCheckAll = (CheckBox)sender;
if (chkCheckAll.Checked == true)
{
foreach (GridViewRow gvr in gvLeaveApproval.Rows)
{
CheckBox chkApprove = (CheckBox)gvr.FindControl("chkApproval");
chkApprove.Checked = true;

GridViewRow row = (GridViewRow)chkApprove.NamingContainer;
GridView grid = (GridView)row.NamingContainer;
string RowId = (grid.DataKeys[row.RowIndex].Value.ToString());
LeaveDetailEntity getLeaveObj = Database.SelectFrom(LeaveDetailEntity.Table)
.Where(LeaveDetailEntity.Table.Id == RowId.ToString())
.LoadEntity<LeaveDetailEntity>();
if (getLeaveObj != null)
{
if (chkApprove.Checked == true)
{
iCount += 1;
getLeaveObj.IsApproval = true;
Script = VetCareUtilities.GetAlert("Approval OK");
Page.ClientScript.RegisterStartupScript(typeof(Page), "approvalok ", Script);
getLeaveObj.Save();
fillLeave(ddlEmployeesName.SelectedValue.ToString());
}
}
chkCheckAll.Checked = true;
ViewState["Count"] = iCount.ToString();
ViewState["GridCount"] = gvLeaveApproval.Rows.Count.ToString();

if (iCount == gvLeaveApproval.Rows.Count)
{
CheckBox chkAll = (CheckBox)gvLeaveApproval.HeaderRow.Cells[6].FindControl("chkCheckAll");
if (chkAll != null)
{
chkAll.Checked = true;
}
}

}
}
else
{
foreach (GridViewRow gvr in gvLeaveApproval.Rows)
{
CheckBox chkApprove = (CheckBox)gvr.FindControl("chkApproval");

GridViewRow row = (GridViewRow)chkApprove.NamingContainer;
GridView grid = (GridView)row.NamingContainer;
string RowId = (grid.DataKeys[row.RowIndex].Value.ToString());
LeaveDetailEntity getLeaveObj = Database.SelectFrom(LeaveDetailEntity.Table)
.Where(LeaveDetailEntity.Table.Id == RowId.ToString())
.LoadEntity<LeaveDetailEntity>();
if (getLeaveObj != null)
{
if (chkApprove.Checked == true)
{
iCount += 1;
getLeaveObj.IsApproval = false;
Script = VetCareUtilities.GetAlert("Approval Cancel");
Page.ClientScript.RegisterStartupScript(typeof(Page), "approvalcancel ", Script);
getLeaveObj.Save();
fillLeave(ddlEmployeesName.SelectedValue.ToString());
}
}
ViewState["Count"] = iCount.ToString();
ViewState["GridCount"] = gvLeaveApproval.Rows.Count.ToString();

if (iCount == gvLeaveApproval.Rows.Count)
{
CheckBox chkAll = (CheckBox)gvLeaveApproval.HeaderRow.Cells[6].FindControl("chkCheckAll");
if (chkAll != null)
{
chkAll.Checked = false;
}
}

}
}
}

protected void gvLeaveApproval_RowDataBound(object sender, GridViewRowEventArgs e)
{
/* Assigning color to Not Approved Leave */
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridViewRow gr = e.Row;
CheckBox chkLeaveApproval = (CheckBox)(e.Row.FindControl("chkApproval"));
if (chkLeaveApproval != null)
{
if (!chkLeaveApproval.Checked)
{
e.Row.BackColor = System.Drawing.Color.Pink;
}
}
}
}
}
}



Author: Chieu    29 Aug 2008Member Level: BronzeRating:     Points: 1
Thanks for all your help.

I have it working now.

Thanks again.


Post Reply
You must Sign In to post a response.
Next : Error: DataBinding: 'System.Char' does not contain a property with the name 'Site Name'.
Previous : code for student forum
Return to Discussion Forum
Post New Message
Category: ASP.NET

Related Messages



dotNet Slackers   BizTalk Adaptors    Web Design

teleconferencing

Contact Us    Privacy Policy    Terms Of Use