nested girdview in datalist cancel update delete c#
<asp:DataList ID="datalist1" Width="100%" DataKeyField="level2taskno" runat="server"
OnItemDataBound="datalist_ItemDataBound">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemTemplate>
<table style="width: 100%; color: Black; font-family: Arial; font-size: 12px;" cellspacing="0">
<tr>
<td>
<table style="width: 100%; color: Black; font-family: Arial; font-size: 12px; border: 1px solid #9a9a9a;"
cellpadding="5">
<tr>
<td bgcolor="#8ce8a0" valign="top" width="7%" style="color: #fff; font-size: 12px;
font-weight: bold;">
<b>Initiative(s):</b>
</td>
<td style="font-family: Arial; font-size: 18px;">
<b>
<asp:Label ID="lbltask" runat="server" Text='<%# Eval("TaskName") %>'></asp:Label></b>
</td>
</tr>
<tr>
<td bgcolor="#9bc1d4" valign="top" style="color: #fff; font-size: 12px; font-weight: bold;
padding-top: 10px;">
<b>Outcome(s):</b>
</td>
<td>
<asp:Literal runat="server" ID="outcome" Text='<%# string.Join("<p style=margin-left:0px; >", Eval("outcome").ToString().Split(new []{(char)13},StringSplitOptions.None)) %>'>
</asp:Literal>
</td>
</tr>
<tr>
<td bgcolor="#e8b880" valign="top" style="color: #fff; font-size: 12px; font-weight: bold;">
<b>Indicators:</b>
</td>
<td>
<asp:Literal runat="server" ID="Literal1" Text='<%# string.Join("<p style=margin-left:0px;> ", Eval("indicators").ToString().Split(new []{(char)13},StringSplitOptions.None)) %>'>
</asp:Literal>
</td>
</tr>
<tr>
<td bgcolor="#a6988f" width="7%" valign="top" style="color: #fff; font-size: 12px;
font-weight: bold;">
<b>Activities: </b>
</td>
<td width="100%">
<div style="width: 100%;">
<asp:GridView ID="GridViewlist" DataKeyNames="level2tasklink,level3taskno" runat="server"
ShowHeader="true" Width="100%" BorderColor="#e2e2e2" AutoGenerateColumns="false"
OnRowCancelingEdit="GridView2_RowCancelingEdit" OnRowDeleting="GridView2_RowDeleting"
OnRowEditing="GridView2_RowEditing" OnRowUpdating="GridView2_RowUpdating" OnRowDataBound="grdQuater1_RowDataBound">
<RowStyle VerticalAlign="Middle" HorizontalAlign="Center" />
<Columns>
<asp:TemplateField HeaderText="No.">
<ItemTemplate>
<asp:HiddenField ID="hidCusState" runat="server" Value='<%# Bind("level3taskno") %>' />
<asp:Label ID="lblautoid" runat="server" Text='<%# Container.DataItemIndex+1 %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Activity">
<ItemTemplate>
<asp:Label ID="lblactivity" runat="server" Text='<%# Eval("Activitydescnational") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="30%" VerticalAlign="Middle" HorizontalAlign="Left" />
<EditItemTemplate>
<asp:TextBox Style="width: 100%;" ID="txtactivity" runat="server" Text='<%# Bind("Activitydescnational") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Outputs">
<ItemTemplate>
<asp:Label ID="lbloutput" runat="server" Text='<%# Eval("output") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="30%" />
<EditItemTemplate>
<asp:TextBox Style="width: 100%;" ID="txtoutput" runat="server" Text='<%# Bind("output") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Global / National">
<ItemTemplate>
<asp:Label ID="lblglobal" runat="server" Text='<%# Eval("global1") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="10%" />
<EditItemTemplate>
<asp:TextBox ID="txtglobal" Style='text-transform: uppercase;' runat="server" Text='<%# Bind("global1") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Activity Type">
<ItemTemplate>
<asp:Label ID="lblactivitytype" runat="server" Text='<%# Eval("mov1") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="10%" />
<EditItemTemplate>
<asp:DropDownList ID="drpmov" AutoPostBack="true" runat="server">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Deadline">
<ItemTemplate>
<asp:Label ID="lblmonth" runat="server" Text='<%# Eval("month_due1") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="10%" />
<EditItemTemplate>
<asp:DropDownList ID="drpdeadline" AutoPostBack="true" runat="server">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Image ID="done" runat="server" Width="25" Height="25" ImageUrl='<%# "~/Images/" + (((bool)Eval("status") == false) ? "NotDone.jpg" : "Done.jpg") %>'>
</asp:Image>
<%-- <asp:Image ID="notdone" runat="server" Width="25" Height="25" Visible="false" ImageUrl="~/images/NotDone.jpg">
</asp:Image>--%>
</ItemTemplate>
<ItemStyle Width="5%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="User">
<ItemTemplate>
<asp:Label ID="lbluser" runat="server" Text='<%# Eval("level3name") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="10%" />
</asp:TemplateField>
<%-- <asp:TemplateField HeaderText="File Reports">
<ItemTemplate>
<asp:LinkButton CommandName='<%# Eval("mov1") %>' ID="lnkreport" ForeColor="#2aa9df"
CommandArgument='<%# Eval("level3taskno") %>' OnClick="LinkButton1_Click" runat="server">File Report</asp:LinkButton>
</ItemTemplate>
<ItemStyle Width="8%" />
</asp:TemplateField>--%>
<asp:CommandField HeaderText="Edit" ShowEditButton="True"></asp:CommandField>
<asp:CommandField HeaderText="Delete" ShowDeleteButton="True">
</asp:CommandField>
</Columns>
<FooterStyle BackColor="#6e6e6e" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="White" VerticalAlign="Middle" HorizontalAlign="Center" />
<SelectedRowStyle HorizontalAlign="Center" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#6e6e6e" Font-Bold="True" ForeColor="White" />
<EditRowStyle />
<AlternatingRowStyle HorizontalAlign="Center" BackColor="White" />
</asp:GridView>
</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
first bind the datalist according to your records
void datalist()
{
sql = "select * from tbl_Task_Objective where level2TaskNo in(select distinct level2TaskLink from tbl_Individual_Task where level3name='" + Request.QueryString["id"].ToString() + "'";
if (drpquarter.SelectedItem.Text != "All")
{
sql = sql + " and quarter='" + drpquarter.SelectedItem.Value + "' ";
}
if (drpinitiative.SelectedItem.Text != "All")
{
sql = sql + " and level2tasklink='" + drpinitiative.SelectedItem.Value + "'";
}
sql = sql + ")";
ds = SqlHelper.ExecuteDataset(Cnn, CommandType.Text, sql);
datalist1.DataSource = ds.Tables[0];
datalist1.DataBind();
}
2. then itemdataboud of datalist
protected void datalist_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string id = (datalist1.DataKeys[e.Item.ItemIndex]).ToString();
GridView gv = (GridView)e.Item.FindControl("GridViewlist");
Label lb1 = (Label)e.Item.FindControl("lbltask");
Label lblmonth = (Label)e.Item.FindControl("lblmonth");
bindgrid(gv, id);
}
3. using bindgrid function
private void bindgrid(GridView GridView, string id)
{
sql = "select *, dbo.GetMonthName(month_due) as month_due1 ,dbo.GetDoneStaus(level3TaskNo) Status1,dbo.GetGlobal(global)as global1,dbo.getmov1(mov)as mov1 from tbl_Individual_Task where level2tasklink='" + id + "' and level3name='" + Request.QueryString["id"].ToString() + "' ";
if (drpquarter.SelectedItem.Text != "All")
{
sql = sql + " and quarter='" + drpquarter.SelectedItem.Value + "' ";
}
if (drpinitiative.SelectedItem.Text != "All")
{
sql = sql + " and level2tasklink='" + drpinitiative.SelectedItem.Value + "'";
}
if (drpglobal.SelectedItem.Text != "All")
{
sql = sql + " and global='" + drpglobal.SelectedItem.Value + "'";
}
ds1 = SqlHelper.ExecuteDataset(Cnn, CommandType.Text, sql);
if (ds1.Tables[0].Rows.Count > 0)
{
GridView.DataSource = ds1.Tables[0];
GridView.DataBind();
}
}
4.gridview rowdatabound event accroding to your requirements
protected void grdQuater1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string item = e.Row.Cells[1].Text;
if (e.Row.Cells[9].HasControls())
{
LinkButton lnkbtnDelete = ((LinkButton)e.Row.Cells[9].Controls[0]);
lnkbtnDelete.Attributes.Add("onclick", "return confirm('Do you want to Delete" + item + "?');");
}
}
if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState & DataControlRowState.Edit) > 0)
{
//string id = (datalist1.DataKeys[e.Row.RowIndex]).ToString();
// object objTemp = GridViewlist.DataKeys[e.Row.RowIndex].Value as object;
GridView gv = (GridView)e.Row.FindControl("GridView2");
Label lb = (Label)e.Row.FindControl("lblno");
Label lb1 = (Label)e.Row.FindControl("lbltask");
Image img = (Image)e.Row.FindControl("done");
// Image img1 = (Image)e.Row.FindControl("notdone");
Label lblmonth = (Label)e.Row.FindControl("lblmonth");
Label lbluser = (Label)e.Row.FindControl("lbluser");
HiddenField hidCusState = (HiddenField)e.Row.FindControl("hidCusState");
string level3taskno = hidCusState.Value;
//LinkButton lnk = (LinkButton)e.Row.FindControl("lnkreport");
//string idd = lnk.CommandArgument;
//sql = "select *, dbo.GetMonthName(month_due) as month_due1 ,dbo.GetDoneStaus(level3TaskNo) Status1,dbo.GetGlobal(global)as global1 from tbl_Individual_Task where quarter=1 and level2tasklink='" + id + "' and level3name='" + Session["Name"].ToString() + "'";
//ds1 = SqlHelper.ExecuteDataset(Cnn, CommandType.Text, sql);
sql = "select *, dbo.GetMonthName(month_due) as month_due1 ,dbo.GetDoneStaus(level3TaskNo) Status1,dbo.GetGlobal(global)as global1,dbo.getmov1(mov)as mov1 from tbl_Individual_Task where level3taskno='" + level3taskno + "' ";
if (drpquarter.SelectedItem.Text != "All")
{
sql = sql + " and quarter='" + drpquarter.SelectedItem.Value + "' ";
}
if (drpinitiative.SelectedItem.Text != "All")
{
sql = sql + " and level2tasklink='" + drpinitiative.SelectedItem.Value + "'";
}
if (drpglobal.SelectedItem.Text != "All")
{
sql = sql + " and global='" + drpglobal.SelectedItem.Value + "'";
}
ds1 = SqlHelper.ExecuteDataset(Cnn, CommandType.Text, sql);
if (ds1.Tables[0].Rows.Count > 0)
{
if (ds1.Tables[0].Rows[0]["status1"].ToString() == "Done")
{
//img.Visible = true;
//img1.Visible = false;
}
else
{
//img.Visible = false;
//img1.Visible = true;
}
}
DropDownList drpdeadline = (DropDownList)e.Row.FindControl("drpdeadline");
sql = "select Monthno,MonthName from tbl_Month_Master order by Monthno ";
ds = SqlHelper.ExecuteDataset(Cnn, CommandType.Text, sql);
if (ds.Tables[0].Rows.Count > 0)
{
drpdeadline.DataSource = ds.Tables[0];
drpdeadline.DataTextField = "MonthName";
drpdeadline.DataValueField = "Monthno";
drpdeadline.DataBind();
drpdeadline.SelectedItem.Text = ds1.Tables[0].Rows[0]["month_due1"].ToString();
// drpdeadline.Items.Insert(0, new ListItem("--Select--", "0"));
}
DropDownList drpmov = (DropDownList)e.Row.FindControl("drpmov");
sql = "select * from tbl_verification_master";
ds = SqlHelper.ExecuteDataset(Cnn, CommandType.Text, sql);
if (ds.Tables[0].Rows.Count > 0)
{
drpmov.DataSource = ds.Tables[0];
drpmov.DataTextField = "description";
drpmov.DataValueField = "id";
drpmov.DataBind();
drpmov.SelectedItem.Text = ds1.Tables[0].Rows[0]["mov1"].ToString();
// drpmov.Items.Insert(0, new ListItem("--Select--", "0"));
}
}
}
5. then using row deleteing updating
protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView GridView1 = (GridView)sender;
GridView1.EditIndex = e.NewEditIndex;
bindgrid(GridView1, GridView1.DataKeys[e.NewEditIndex][0].ToString());
}
protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridView GridView1 = (GridView)sender;
TextBox txtactivity = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtactivity");
TextBox txtoutput = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtoutput");
TextBox txtglobal = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtglobal");//.Cells[1].Controls[0];
DropDownList drpdeadline = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("drpdeadline");
DropDownList drpmov = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("drpmov");
sql = "select Monthno,MonthName from tbl_Month_Master where monthname='" + drpdeadline.SelectedItem.Text + "' ";
ds = SqlHelper.ExecuteDataset(Cnn, CommandType.Text, sql);
if (ds.Tables[0].Rows.Count > 0)
{
drpdeadline.DataSource = ds.Tables[0];
drpdeadline.DataTextField = "MonthName";
drpdeadline.DataValueField = "Monthno";
drpdeadline.DataBind();
}
string drp = drpdeadline.SelectedItem.Text;
string drpdeadlinevalue = drpdeadline.SelectedValue;
sql = "select * from tbl_verification_master where description='" + drpmov.SelectedItem.Text + "'";
ds = SqlHelper.ExecuteDataset(Cnn, CommandType.Text, sql);
if (ds.Tables[0].Rows.Count > 0)
{
drpmov.DataSource = ds.Tables[0];
drpmov.DataTextField = "description";
drpmov.DataValueField = "id";
drpmov.DataBind();
}
string drpmovvalue = drpmov.SelectedValue;
txtglobal.Text = txtglobal.Text.ToUpper();
if (txtglobal.Text == "GLOBAL")
{
txtglobal.Text = "2";
}
else if (txtglobal.Text == "INDIA")
{
txtglobal.Text = "1";
}
else
{
txtglobal.Text = "";
}
sql = "update tbl_Individual_Task set Activitydescnational='" + txtactivity.Text + "', output='" + txtoutput.Text + "', global='" + txtglobal.Text + "',month_due='" + drpdeadlinevalue + "', mov='" + drpmovvalue + "' where level3taskno='" + GridView1.DataKeys[e.RowIndex].Values[1].ToString() + "'";
SqlHelper.ExecuteNonQuery(Cnn, CommandType.Text, sql);
GridView1.EditIndex = -1;
bindgrid(GridView1, GridView1.DataKeys[e.RowIndex][0].ToString());
}
protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridView GridView1 = (GridView)sender;
sql = "delete tbl_Individual_Task where level3taskno='" + GridView1.DataKeys[e.RowIndex].Values[1].ToString() + "'";
// SqlHelper.ExecuteNonQuery(Cnn, CommandType.Text, sql);
bindgrid(GridView1, GridView1.DataKeys[e.RowIndex][0].ToString());
}
protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView GridView1 = (GridView)sender;
GridView1.EditIndex = -1;
bindgrid(GridView1, GridView1.DataKeys[e.RowIndex][0].ToString());
}
OnItemDataBound="datalist_ItemDataBound">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemTemplate>
<table style="width: 100%; color: Black; font-family: Arial; font-size: 12px;" cellspacing="0">
<tr>
<td>
<table style="width: 100%; color: Black; font-family: Arial; font-size: 12px; border: 1px solid #9a9a9a;"
cellpadding="5">
<tr>
<td bgcolor="#8ce8a0" valign="top" width="7%" style="color: #fff; font-size: 12px;
font-weight: bold;">
<b>Initiative(s):</b>
</td>
<td style="font-family: Arial; font-size: 18px;">
<b>
<asp:Label ID="lbltask" runat="server" Text='<%# Eval("TaskName") %>'></asp:Label></b>
</td>
</tr>
<tr>
<td bgcolor="#9bc1d4" valign="top" style="color: #fff; font-size: 12px; font-weight: bold;
padding-top: 10px;">
<b>Outcome(s):</b>
</td>
<td>
<asp:Literal runat="server" ID="outcome" Text='<%# string.Join("<p style=margin-left:0px; >", Eval("outcome").ToString().Split(new []{(char)13},StringSplitOptions.None)) %>'>
</asp:Literal>
</td>
</tr>
<tr>
<td bgcolor="#e8b880" valign="top" style="color: #fff; font-size: 12px; font-weight: bold;">
<b>Indicators:</b>
</td>
<td>
<asp:Literal runat="server" ID="Literal1" Text='<%# string.Join("<p style=margin-left:0px;> ", Eval("indicators").ToString().Split(new []{(char)13},StringSplitOptions.None)) %>'>
</asp:Literal>
</td>
</tr>
<tr>
<td bgcolor="#a6988f" width="7%" valign="top" style="color: #fff; font-size: 12px;
font-weight: bold;">
<b>Activities: </b>
</td>
<td width="100%">
<div style="width: 100%;">
<asp:GridView ID="GridViewlist" DataKeyNames="level2tasklink,level3taskno" runat="server"
ShowHeader="true" Width="100%" BorderColor="#e2e2e2" AutoGenerateColumns="false"
OnRowCancelingEdit="GridView2_RowCancelingEdit" OnRowDeleting="GridView2_RowDeleting"
OnRowEditing="GridView2_RowEditing" OnRowUpdating="GridView2_RowUpdating" OnRowDataBound="grdQuater1_RowDataBound">
<RowStyle VerticalAlign="Middle" HorizontalAlign="Center" />
<Columns>
<asp:TemplateField HeaderText="No.">
<ItemTemplate>
<asp:HiddenField ID="hidCusState" runat="server" Value='<%# Bind("level3taskno") %>' />
<asp:Label ID="lblautoid" runat="server" Text='<%# Container.DataItemIndex+1 %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Activity">
<ItemTemplate>
<asp:Label ID="lblactivity" runat="server" Text='<%# Eval("Activitydescnational") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="30%" VerticalAlign="Middle" HorizontalAlign="Left" />
<EditItemTemplate>
<asp:TextBox Style="width: 100%;" ID="txtactivity" runat="server" Text='<%# Bind("Activitydescnational") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Outputs">
<ItemTemplate>
<asp:Label ID="lbloutput" runat="server" Text='<%# Eval("output") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="30%" />
<EditItemTemplate>
<asp:TextBox Style="width: 100%;" ID="txtoutput" runat="server" Text='<%# Bind("output") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Global / National">
<ItemTemplate>
<asp:Label ID="lblglobal" runat="server" Text='<%# Eval("global1") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="10%" />
<EditItemTemplate>
<asp:TextBox ID="txtglobal" Style='text-transform: uppercase;' runat="server" Text='<%# Bind("global1") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Activity Type">
<ItemTemplate>
<asp:Label ID="lblactivitytype" runat="server" Text='<%# Eval("mov1") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="10%" />
<EditItemTemplate>
<asp:DropDownList ID="drpmov" AutoPostBack="true" runat="server">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Deadline">
<ItemTemplate>
<asp:Label ID="lblmonth" runat="server" Text='<%# Eval("month_due1") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="10%" />
<EditItemTemplate>
<asp:DropDownList ID="drpdeadline" AutoPostBack="true" runat="server">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Image ID="done" runat="server" Width="25" Height="25" ImageUrl='<%# "~/Images/" + (((bool)Eval("status") == false) ? "NotDone.jpg" : "Done.jpg") %>'>
</asp:Image>
<%-- <asp:Image ID="notdone" runat="server" Width="25" Height="25" Visible="false" ImageUrl="~/images/NotDone.jpg">
</asp:Image>--%>
</ItemTemplate>
<ItemStyle Width="5%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="User">
<ItemTemplate>
<asp:Label ID="lbluser" runat="server" Text='<%# Eval("level3name") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="10%" />
</asp:TemplateField>
<%-- <asp:TemplateField HeaderText="File Reports">
<ItemTemplate>
<asp:LinkButton CommandName='<%# Eval("mov1") %>' ID="lnkreport" ForeColor="#2aa9df"
CommandArgument='<%# Eval("level3taskno") %>' OnClick="LinkButton1_Click" runat="server">File Report</asp:LinkButton>
</ItemTemplate>
<ItemStyle Width="8%" />
</asp:TemplateField>--%>
<asp:CommandField HeaderText="Edit" ShowEditButton="True"></asp:CommandField>
<asp:CommandField HeaderText="Delete" ShowDeleteButton="True">
</asp:CommandField>
</Columns>
<FooterStyle BackColor="#6e6e6e" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="White" VerticalAlign="Middle" HorizontalAlign="Center" />
<SelectedRowStyle HorizontalAlign="Center" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#6e6e6e" Font-Bold="True" ForeColor="White" />
<EditRowStyle />
<AlternatingRowStyle HorizontalAlign="Center" BackColor="White" />
</asp:GridView>
</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
first bind the datalist according to your records
void datalist()
{
sql = "select * from tbl_Task_Objective where level2TaskNo in(select distinct level2TaskLink from tbl_Individual_Task where level3name='" + Request.QueryString["id"].ToString() + "'";
if (drpquarter.SelectedItem.Text != "All")
{
sql = sql + " and quarter='" + drpquarter.SelectedItem.Value + "' ";
}
if (drpinitiative.SelectedItem.Text != "All")
{
sql = sql + " and level2tasklink='" + drpinitiative.SelectedItem.Value + "'";
}
sql = sql + ")";
ds = SqlHelper.ExecuteDataset(Cnn, CommandType.Text, sql);
datalist1.DataSource = ds.Tables[0];
datalist1.DataBind();
}
2. then itemdataboud of datalist
protected void datalist_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string id = (datalist1.DataKeys[e.Item.ItemIndex]).ToString();
GridView gv = (GridView)e.Item.FindControl("GridViewlist");
Label lb1 = (Label)e.Item.FindControl("lbltask");
Label lblmonth = (Label)e.Item.FindControl("lblmonth");
bindgrid(gv, id);
}
3. using bindgrid function
private void bindgrid(GridView GridView, string id)
{
sql = "select *, dbo.GetMonthName(month_due) as month_due1 ,dbo.GetDoneStaus(level3TaskNo) Status1,dbo.GetGlobal(global)as global1,dbo.getmov1(mov)as mov1 from tbl_Individual_Task where level2tasklink='" + id + "' and level3name='" + Request.QueryString["id"].ToString() + "' ";
if (drpquarter.SelectedItem.Text != "All")
{
sql = sql + " and quarter='" + drpquarter.SelectedItem.Value + "' ";
}
if (drpinitiative.SelectedItem.Text != "All")
{
sql = sql + " and level2tasklink='" + drpinitiative.SelectedItem.Value + "'";
}
if (drpglobal.SelectedItem.Text != "All")
{
sql = sql + " and global='" + drpglobal.SelectedItem.Value + "'";
}
ds1 = SqlHelper.ExecuteDataset(Cnn, CommandType.Text, sql);
if (ds1.Tables[0].Rows.Count > 0)
{
GridView.DataSource = ds1.Tables[0];
GridView.DataBind();
}
}
4.gridview rowdatabound event accroding to your requirements
protected void grdQuater1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string item = e.Row.Cells[1].Text;
if (e.Row.Cells[9].HasControls())
{
LinkButton lnkbtnDelete = ((LinkButton)e.Row.Cells[9].Controls[0]);
lnkbtnDelete.Attributes.Add("onclick", "return confirm('Do you want to Delete" + item + "?');");
}
}
if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState & DataControlRowState.Edit) > 0)
{
//string id = (datalist1.DataKeys[e.Row.RowIndex]).ToString();
// object objTemp = GridViewlist.DataKeys[e.Row.RowIndex].Value as object;
GridView gv = (GridView)e.Row.FindControl("GridView2");
Label lb = (Label)e.Row.FindControl("lblno");
Label lb1 = (Label)e.Row.FindControl("lbltask");
Image img = (Image)e.Row.FindControl("done");
// Image img1 = (Image)e.Row.FindControl("notdone");
Label lblmonth = (Label)e.Row.FindControl("lblmonth");
Label lbluser = (Label)e.Row.FindControl("lbluser");
HiddenField hidCusState = (HiddenField)e.Row.FindControl("hidCusState");
string level3taskno = hidCusState.Value;
//LinkButton lnk = (LinkButton)e.Row.FindControl("lnkreport");
//string idd = lnk.CommandArgument;
//sql = "select *, dbo.GetMonthName(month_due) as month_due1 ,dbo.GetDoneStaus(level3TaskNo) Status1,dbo.GetGlobal(global)as global1 from tbl_Individual_Task where quarter=1 and level2tasklink='" + id + "' and level3name='" + Session["Name"].ToString() + "'";
//ds1 = SqlHelper.ExecuteDataset(Cnn, CommandType.Text, sql);
sql = "select *, dbo.GetMonthName(month_due) as month_due1 ,dbo.GetDoneStaus(level3TaskNo) Status1,dbo.GetGlobal(global)as global1,dbo.getmov1(mov)as mov1 from tbl_Individual_Task where level3taskno='" + level3taskno + "' ";
if (drpquarter.SelectedItem.Text != "All")
{
sql = sql + " and quarter='" + drpquarter.SelectedItem.Value + "' ";
}
if (drpinitiative.SelectedItem.Text != "All")
{
sql = sql + " and level2tasklink='" + drpinitiative.SelectedItem.Value + "'";
}
if (drpglobal.SelectedItem.Text != "All")
{
sql = sql + " and global='" + drpglobal.SelectedItem.Value + "'";
}
ds1 = SqlHelper.ExecuteDataset(Cnn, CommandType.Text, sql);
if (ds1.Tables[0].Rows.Count > 0)
{
if (ds1.Tables[0].Rows[0]["status1"].ToString() == "Done")
{
//img.Visible = true;
//img1.Visible = false;
}
else
{
//img.Visible = false;
//img1.Visible = true;
}
}
DropDownList drpdeadline = (DropDownList)e.Row.FindControl("drpdeadline");
sql = "select Monthno,MonthName from tbl_Month_Master order by Monthno ";
ds = SqlHelper.ExecuteDataset(Cnn, CommandType.Text, sql);
if (ds.Tables[0].Rows.Count > 0)
{
drpdeadline.DataSource = ds.Tables[0];
drpdeadline.DataTextField = "MonthName";
drpdeadline.DataValueField = "Monthno";
drpdeadline.DataBind();
drpdeadline.SelectedItem.Text = ds1.Tables[0].Rows[0]["month_due1"].ToString();
// drpdeadline.Items.Insert(0, new ListItem("--Select--", "0"));
}
DropDownList drpmov = (DropDownList)e.Row.FindControl("drpmov");
sql = "select * from tbl_verification_master";
ds = SqlHelper.ExecuteDataset(Cnn, CommandType.Text, sql);
if (ds.Tables[0].Rows.Count > 0)
{
drpmov.DataSource = ds.Tables[0];
drpmov.DataTextField = "description";
drpmov.DataValueField = "id";
drpmov.DataBind();
drpmov.SelectedItem.Text = ds1.Tables[0].Rows[0]["mov1"].ToString();
// drpmov.Items.Insert(0, new ListItem("--Select--", "0"));
}
}
}
5. then using row deleteing updating
protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView GridView1 = (GridView)sender;
GridView1.EditIndex = e.NewEditIndex;
bindgrid(GridView1, GridView1.DataKeys[e.NewEditIndex][0].ToString());
}
protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridView GridView1 = (GridView)sender;
TextBox txtactivity = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtactivity");
TextBox txtoutput = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtoutput");
TextBox txtglobal = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtglobal");//.Cells[1].Controls[0];
DropDownList drpdeadline = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("drpdeadline");
DropDownList drpmov = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("drpmov");
sql = "select Monthno,MonthName from tbl_Month_Master where monthname='" + drpdeadline.SelectedItem.Text + "' ";
ds = SqlHelper.ExecuteDataset(Cnn, CommandType.Text, sql);
if (ds.Tables[0].Rows.Count > 0)
{
drpdeadline.DataSource = ds.Tables[0];
drpdeadline.DataTextField = "MonthName";
drpdeadline.DataValueField = "Monthno";
drpdeadline.DataBind();
}
string drp = drpdeadline.SelectedItem.Text;
string drpdeadlinevalue = drpdeadline.SelectedValue;
sql = "select * from tbl_verification_master where description='" + drpmov.SelectedItem.Text + "'";
ds = SqlHelper.ExecuteDataset(Cnn, CommandType.Text, sql);
if (ds.Tables[0].Rows.Count > 0)
{
drpmov.DataSource = ds.Tables[0];
drpmov.DataTextField = "description";
drpmov.DataValueField = "id";
drpmov.DataBind();
}
string drpmovvalue = drpmov.SelectedValue;
txtglobal.Text = txtglobal.Text.ToUpper();
if (txtglobal.Text == "GLOBAL")
{
txtglobal.Text = "2";
}
else if (txtglobal.Text == "INDIA")
{
txtglobal.Text = "1";
}
else
{
txtglobal.Text = "";
}
sql = "update tbl_Individual_Task set Activitydescnational='" + txtactivity.Text + "', output='" + txtoutput.Text + "', global='" + txtglobal.Text + "',month_due='" + drpdeadlinevalue + "', mov='" + drpmovvalue + "' where level3taskno='" + GridView1.DataKeys[e.RowIndex].Values[1].ToString() + "'";
SqlHelper.ExecuteNonQuery(Cnn, CommandType.Text, sql);
GridView1.EditIndex = -1;
bindgrid(GridView1, GridView1.DataKeys[e.RowIndex][0].ToString());
}
protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridView GridView1 = (GridView)sender;
sql = "delete tbl_Individual_Task where level3taskno='" + GridView1.DataKeys[e.RowIndex].Values[1].ToString() + "'";
// SqlHelper.ExecuteNonQuery(Cnn, CommandType.Text, sql);
bindgrid(GridView1, GridView1.DataKeys[e.RowIndex][0].ToString());
}
protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView GridView1 = (GridView)sender;
GridView1.EditIndex = -1;
bindgrid(GridView1, GridView1.DataKeys[e.RowIndex][0].ToString());
}
Comments
Post a Comment