Design of Grid View
-----------------------
In the Grid View, we have three text boxes, and three drop down lists, to get Understand that
how to perform this action with text boxes and Drop down lists, i have implemented this.
View Source :
--------------
<asp:UpdatePanel ID="UpdatePanel1" runat="server" >
<ContentTemplate>
<div style="text-align:left;margin-left:50px">
<asp:gridview ID="Gridview1" runat="server" ShowFooter="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" GridLines="Vertical" >
<AlternatingRowStyle BackColor="#CCCCCC" />
<Columns >
<asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<asp:TextBox ID="txtFirstName" runat="server" Height="25px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<asp:TextBox ID="txtLastName" runat="server" Height="25px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date">
<ItemTemplate>
<asp:TextBox ID="txtDate" runat="server" AutoPostBack="true" class="date" Height="25px">
</asp:TextBox>
</ItemTemplate></asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="ddlAgeRange" runat="server">
<asp:ListItem Value="-1">Age Range</asp:ListItem>
</asp:DropDownList>
</ItemTemplate></asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="ddlRelationship" runat="server">
<asp:ListItem Value="-1">Relationship</asp:ListItem>
</asp:DropDownList>
</ItemTemplate></asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="ddlGender" runat="server">
<asp:ListItem Value="-1">Gender</asp:ListItem>
</asp:DropDownList>
</ItemTemplate></asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:gridview></div><br />
<asp:LinkButton ID="lnkMore" runat="server" Text="More(>>)"></asp:LinkButton>
<asp:LinkButton ID="lnkLess" runat="server" Text="Less(<<)"></asp:LinkButton>
<br /><br />
</ContentTemplate></asp:UpdatePanel>
To Increase and Decrease the rows
//Set Intial Row to Grid view
public void SetInitialRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
dt.Columns.Add(new DataColumn("Column1", typeof(string)));
dt.Columns.Add(new DataColumn("Column2", typeof(string)));
dt.Columns.Add(new DataColumn("Column3", typeof(string)));
dt.Columns.Add(new DataColumn("Column4", typeof(string)));
dt.Columns.Add(new DataColumn("Column5", typeof(string)));
dt.Columns.Add(new DataColumn("Column6", typeof(string)));
dr = dt.NewRow();
dr["RowNumber"] = 1;
dt.Rows.Add(dr);
ViewState["CurrentTable"] = dt;
Gridview1.DataSource = dt;
Gridview1.DataBind();
DropDownList ddl1 = (DropDownList)Gridview1.Rows[0].Cells[1].FindControl("ddlAgeRange");
DropDownList ddl2 = (DropDownList)Gridview1.Rows[0].Cells[2].FindControl("ddlRelationship");
DropDownList ddl3 = (DropDownList)Gridview1.Rows[0].Cells[3].FindControl("ddlGender");
FillDdlAgeRange(ddl1);
FillDdlRelationship(ddl2);
FillDdlGender(ddl3);
}
//Adding Rows to the Grid View
private void AddNewRowToGrid()
{
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = dtCurrentTable.Rows.Count + 1;
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
for (int i = 0; i < dtCurrentTable.Rows.Count - 1; i++)
{
TextBox box1 = (TextBox)Gridview1.Rows[i].Cells[1].FindControl("txtFirstName");
TextBox box2 = (TextBox)Gridview1.Rows[i].Cells[2].FindControl("txtLastName");
TextBox box3 = (TextBox)Gridview1.Rows[i].Cells[3].FindControl("txtDate");
DropDownList ddl1 = (DropDownList)Gridview1.Rows[i].Cells[4].FindControl("ddlAgeRange");
DropDownList ddl2 = (DropDownList)Gridview1.Rows[i].Cells[5].FindControl("ddlRelationship");
DropDownList ddl3 = (DropDownList)Gridview1.Rows[i].Cells[6].FindControl("ddlGender");
dtCurrentTable.Rows[i]["Column1"] = box1.Text;
dtCurrentTable.Rows[i]["Column2"] = box2.Text;
dtCurrentTable.Rows[i]["Column3"] = box3.Text;
dtCurrentTable.Rows[i]["Column4"] = ddl1.SelectedItem.Text;
dtCurrentTable.Rows[i]["Column5"] = ddl2.SelectedItem.Text;
dtCurrentTable.Rows[i]["Column6"] = ddl3.SelectedItem.Text;
}
Gridview1.DataSource = dtCurrentTable;
Gridview1.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
SetPreviousData();
}
//Deleting the rows of Grid View
private void DeleteRowFromGrid()
{
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = dtCurrentTable.Rows.Count - 1; i > 0; i--)
{
for (int j = dtCurrentTable.Rows.Count - 1; j >= i; j--)
{
dtCurrentTable.Rows[j].Delete();
}
break;
}
Gridview1.DataSource = dtCurrentTable;
Gridview1.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
SetPreviousData();
}
// Fires when click on More to add rows
protected void lnkMore_Click(object sender, EventArgs e)
{
AddNewRowToGrid();
}
// Fires when click on Less to Delete rows
protected void lnkLess_Click(object sender, EventArgs e)
{
DeleteRowFromGrid();
}
Note : Do You Know another technique to do this.
Then what are you waiting for , mail to dotnetcircle@gmail.com to post in this Blog.
Happy Coding.
-----------------------
In the Grid View, we have three text boxes, and three drop down lists, to get Understand that
how to perform this action with text boxes and Drop down lists, i have implemented this.
View Source :
--------------
<asp:UpdatePanel ID="UpdatePanel1" runat="server" >
<ContentTemplate>
<div style="text-align:left;margin-left:50px">
<asp:gridview ID="Gridview1" runat="server" ShowFooter="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" GridLines="Vertical" >
<AlternatingRowStyle BackColor="#CCCCCC" />
<Columns >
<asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<asp:TextBox ID="txtFirstName" runat="server" Height="25px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<asp:TextBox ID="txtLastName" runat="server" Height="25px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date">
<ItemTemplate>
<asp:TextBox ID="txtDate" runat="server" AutoPostBack="true" class="date" Height="25px">
</asp:TextBox>
</ItemTemplate></asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="ddlAgeRange" runat="server">
<asp:ListItem Value="-1">Age Range</asp:ListItem>
</asp:DropDownList>
</ItemTemplate></asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="ddlRelationship" runat="server">
<asp:ListItem Value="-1">Relationship</asp:ListItem>
</asp:DropDownList>
</ItemTemplate></asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="ddlGender" runat="server">
<asp:ListItem Value="-1">Gender</asp:ListItem>
</asp:DropDownList>
</ItemTemplate></asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:gridview></div><br />
<asp:LinkButton ID="lnkMore" runat="server" Text="More(>>)"></asp:LinkButton>
<asp:LinkButton ID="lnkLess" runat="server" Text="Less(<<)"></asp:LinkButton>
<br /><br />
</ContentTemplate></asp:UpdatePanel>
To Increase and Decrease the rows
//Set Intial Row to Grid view
public void SetInitialRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
dt.Columns.Add(new DataColumn("Column1", typeof(string)));
dt.Columns.Add(new DataColumn("Column2", typeof(string)));
dt.Columns.Add(new DataColumn("Column3", typeof(string)));
dt.Columns.Add(new DataColumn("Column4", typeof(string)));
dt.Columns.Add(new DataColumn("Column5", typeof(string)));
dt.Columns.Add(new DataColumn("Column6", typeof(string)));
dr = dt.NewRow();
dr["RowNumber"] = 1;
dt.Rows.Add(dr);
ViewState["CurrentTable"] = dt;
Gridview1.DataSource = dt;
Gridview1.DataBind();
DropDownList ddl1 = (DropDownList)Gridview1.Rows[0].Cells[1].FindControl("ddlAgeRange");
DropDownList ddl2 = (DropDownList)Gridview1.Rows[0].Cells[2].FindControl("ddlRelationship");
DropDownList ddl3 = (DropDownList)Gridview1.Rows[0].Cells[3].FindControl("ddlGender");
FillDdlAgeRange(ddl1);
FillDdlRelationship(ddl2);
FillDdlGender(ddl3);
}
//Adding Rows to the Grid View
private void AddNewRowToGrid()
{
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = dtCurrentTable.Rows.Count + 1;
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
for (int i = 0; i < dtCurrentTable.Rows.Count - 1; i++)
{
TextBox box1 = (TextBox)Gridview1.Rows[i].Cells[1].FindControl("txtFirstName");
TextBox box2 = (TextBox)Gridview1.Rows[i].Cells[2].FindControl("txtLastName");
TextBox box3 = (TextBox)Gridview1.Rows[i].Cells[3].FindControl("txtDate");
DropDownList ddl1 = (DropDownList)Gridview1.Rows[i].Cells[4].FindControl("ddlAgeRange");
DropDownList ddl2 = (DropDownList)Gridview1.Rows[i].Cells[5].FindControl("ddlRelationship");
DropDownList ddl3 = (DropDownList)Gridview1.Rows[i].Cells[6].FindControl("ddlGender");
dtCurrentTable.Rows[i]["Column1"] = box1.Text;
dtCurrentTable.Rows[i]["Column2"] = box2.Text;
dtCurrentTable.Rows[i]["Column3"] = box3.Text;
dtCurrentTable.Rows[i]["Column4"] = ddl1.SelectedItem.Text;
dtCurrentTable.Rows[i]["Column5"] = ddl2.SelectedItem.Text;
dtCurrentTable.Rows[i]["Column6"] = ddl3.SelectedItem.Text;
}
Gridview1.DataSource = dtCurrentTable;
Gridview1.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
SetPreviousData();
}
//Deleting the rows of Grid View
private void DeleteRowFromGrid()
{
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = dtCurrentTable.Rows.Count - 1; i > 0; i--)
{
for (int j = dtCurrentTable.Rows.Count - 1; j >= i; j--)
{
dtCurrentTable.Rows[j].Delete();
}
break;
}
Gridview1.DataSource = dtCurrentTable;
Gridview1.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
SetPreviousData();
}
// Fires when click on More to add rows
protected void lnkMore_Click(object sender, EventArgs e)
{
AddNewRowToGrid();
}
// Fires when click on Less to Delete rows
protected void lnkLess_Click(object sender, EventArgs e)
{
DeleteRowFromGrid();
}
Note : Do You Know another technique to do this.
Then what are you waiting for , mail to dotnetcircle@gmail.com to post in this Blog.
Happy Coding.
No comments:
Post a Comment