Subhash Sharma

Subhash Sharma
Subhash Sharma

This is Subhash Sharma(Software Engineer) Blog

Welcome to this blog and find every solution.............

Search This Blog

Software Engineer(Subhash Sharma)

Software Engineer(Subhash Sharma)
Software Engineer

Friday, February 18, 2011

Grid View with check boxe Insert all the vlue in maping tble

//DataBase

//first table

CREATE TABLE tbluser(
[UId] [int] IDENTITY(1,1) NOT NULL,
[usreId] [varchar](100) CONSTRAINT [pkuser] PRIMARY KEY CLUSTERED ,
[UserName] [varchar](100)
)
//second table

CREATE TABLE tblGroup(
[GroupId] [int] IDENTITY(1,1)CONSTRAINT [pkGroup] PRIMARY KEY CLUSTERED ,
[GroupName] [varchar](100) )

//third table

CREATE TABLE MapUserGroup(
[usreId] [varchar](100) FOREIGN KEY([Groupid]) REFERENCES [dbo].[tblGroup] ([GroupId]),
[Groupid] [int] FOREIGN KEY([usreId]) REFERENCES [dbo].[tbluser] ([usreId]))


//.cs code

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Master"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var sql = "select GroupId,GroupName from tblGroup";
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand(sql, con);
da.Fill(ds);
if (ds.Tables.Count > 0)
{
gUser.DataSource = ds;
gUser.DataBind();
}
unCheckUserGrid();
}

}
private void unCheckUserGrid()
{
for (int groupcount = 0; groupcount < gUser.Rows.Count; groupcount++) { CheckBox chkGroup = (CheckBox)gUser.Rows[groupcount].FindControl("chkUser"); chkGroup.Checked = false; } } protected void gUser_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.Header) { CheckBox chkGHeader = (CheckBox)e.Row.FindControl("chkUHeader"); chkGHeader.Attributes.Add("onclick", "return maintainState('" + chkGHeader.ClientID + "','" + gUser.ClientID + "');"); } if (e.Row.RowIndex >= 0)
{
CheckBox chkGroup = (CheckBox)e.Row.FindControl("chkUser");
chkGroup.Attributes.Add("onclick", "return uncheckHeader('" + gUser.ClientID + "');");
}
}

string sql = string.Empty;
private void saveall()
{

ArrayList arr = new ArrayList();
CheckBox chk;
Label lbl;
int i;
foreach (GridViewRow d in gUser.Rows)
{
chk = (CheckBox)d.FindControl("chkUser");

if (chk.Checked)
{
lbl = (Label)d.FindControl("lblGroupId");
arr.Add(lbl.Text);
}
}


string userId = txtUserName.Text.Trim().Replace("'", "''");
SqlDataAdapter adp = new SqlDataAdapter("Select usreId from tbluser where usreId='" + userId + "'", con);
DataSet ds = new DataSet();
adp.Fill(ds);
SqlCommand cmd ;
if (ds.Tables[0].Rows.Count > 0)
{
string UId = ds.Tables[0].Rows[0]["usreId"].ToString();
if (arr.Count > 0)
{
for (i = 0; i <= arr.Count - 1; i++) { sql += "insert into MapUserGroup(usreId,groupid) values('" + UId + "','" + arr[i] + "')"; } con.Open(); cmd = new SqlCommand(sql, con); cmd.ExecuteNonQuery(); con.Close(); unCheckUserGrid(); } } } protected void BtnSave_Click(object sender, EventArgs e) { con.Open(); SqlCommand cmd = new SqlCommand("insert into tbluser(usreId) values('" + txtUserName.Text.Trim().Replace("'", "''") + "')", con); cmd.ExecuteNonQuery(); con.Close(); //after save in maping value saveall(); } //Design .aspx page table align="center" >
tr>
td>
span>UserName /span>
/td>
td>
asp:TextBox runat="server" ID="txtUserName" > /asp:TextBox>
/td>
/tr>
/table>
table align="center" >
tr>
td>
div style="overflow: auto">
asp:GridView ID="gUser" AutoGenerateColumns="False"
runat="server"
BackColor="White" BorderColor="#999999" BorderStyle="Double"
BorderWidth="1px" CellPadding="0" Width="100%" Font-Overline="False"
Font-Size="Small" HorizontalAlign="Center" onrowdatabound="gUser_RowDataBound" >
FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
RowStyle BackColor="#EEEEEE" ForeColor="Black" />
Columns>
asp:TemplateField>
HeaderTemplate>
asp:CheckBox ID="chkUHeader" runat="server" />
/HeaderTemplate>
ItemTemplate>
asp:CheckBox ID="chkUser" runat="server" />
/ItemTemplate>
ItemStyle Width="10%" />
/asp:TemplateField>
asp:TemplateField HeaderText="Group Id" HeaderStyle-HorizontalAlign="Left">
ItemTemplate>
asp:Label ID="lblGroupId" runat="server" Font-Size="100%" Text='<%# Bind("GroupId") %>' >
/ItemTemplate>

HeaderStyle HorizontalAlign="Left">
/asp:TemplateField>


asp:TemplateField HeaderText="Group Name" HeaderStyle-HorizontalAlign="Left">
ItemTemplate>
asp:Label ID="lblGroupName" Font-Size="100%" runat="server" Text='<%# Bind("GroupName") %>'>
/ItemTemplate>

HeaderStyle HorizontalAlign="Left">
/asp:TemplateField>

/Columns>
SelectedRowStyle BackColor="#008A8C" ForeColor="White" />
HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" BorderStyle="Double" Font-Size="Small"
HorizontalAlign="Left" />
AlternatingRowStyle BackColor="#DCDCDC" />
/asp:GridView>

/div>

/td>
/tr>
tr>
td align="right" >
asp:Button ID="BtnSave" runat="server" onclick="BtnSave_Click" Text="Save" />
/td>
/tr>
/table>

//.Js file

function maintainState(checkall,Grid)
{
var gvTable=document.getElementById(Grid);
var control=document.getElementById(checkall);

for(var i=1;i {
if(control.checked==true)
{
gvTable.rows[i].cells[0].childNodes[0].checked=true;
}
else
{
gvTable.rows[i].cells[0].childNodes[0].checked=false;
}
}
}

function uncheckHeader(control)
{
document.getElementById(control).rows[0].cells[0].childNodes[0].checked=false;

}

No comments:

Post a Comment