Sample Design:
Design coding:
<asp:GridView ID="grdMateriallist" runat="server" Width="981px"
AutoGenerateColumns="False" onrowcommand="grdMateriallist_RowCommand"
onrowdeleting="grdMateriallist_RowDeleting"
onrowediting="grdMateriallist_RowEditing">
<Columns>
<asp:BoundField DataField="rmname" HeaderText="Material Name" />
<asp:BoundField DataField="icode" HeaderText="Item Code" />
<asp:TemplateField HeaderText="Material Code" Visible="False">
<ItemTemplate>
<asp:Label ID="rmcode" runat="server" Text='<%#(Eval("rmcode")) %>'> </asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="rmqty" HeaderText="Material Qty" />
<asp:BoundField DataField="uom" HeaderText="UOM" />
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:LinkButton ID="Edit" runat="server" CausesValidation="false"
CommandArgument="Edit" CommandName="Edit">Edit</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton ID="Delete" runat="server" CausesValidation="false"
CommandArgument="Delete" CommandName="Delete"
OnClientClick="return confirm('Are you sure to delete this row?');">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In Page Load :
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GrdRawMaterial();
}
}
To add New Function :
private void GrdRawMaterial()
{
DataTable dt = new DataTable();
dt.Columns.Add("rmname");
dt.Columns.Add("icode");
dt.Columns.Add("rmcode");
dt.Columns.Add("rmqty");
dt.Columns.Add("uom");
ViewState["TempTable"] = dt;
}
To Click Add Button:
string rmname = DrpMName.SelectedItem.Text.ToString().Trim();
string icode = DrpMCode.SelectedItem.Text.ToString().Trim();
string rmcode1 = DrpMName.SelectedValue.ToString().Trim();
string uom = txtUom.Text.ToString().Trim();
string qty1 = txtKLQuantity.Text.ToString().Trim();
double qty = 0;
if (qty1 != "")
{
qty = Convert.ToDouble(qty1);
}
int rmcode = 0;
if (rmcode1 != "")
{
rmcode = Convert.ToInt32(rmcode1);
}
int rowid = -1;
if (lblRowno.Text != "")
{
rowid = Convert.ToInt32(lblRowno.Text.ToString());
}
DataTable dt = new DataTable();
DataRow dr;
if (ViewState["TempTable"] == null)
{
dt.Columns.Add("rmname");
dt.Columns.Add("icode");
dt.Columns.Add("rmcode");
dt.Columns.Add("rmqty");
dt.Columns.Add("uom");
}
else
{
dt = (DataTable)ViewState["TempTable"];
}
if (rowid == -1)
{
dr = dt.NewRow();
dr["rmname"] = rmname;
dr["icode"] = icode;
dr["rmcode"] = rmcode;
dr["rmqty"] = qty;
dr["uom"] = uom;
dt.Rows.Add(dr);
ViewState["TempTable"] = dt;
grdMateriallist.DataSource = (DataTable)ViewState["TempTable"];
grdMateriallist.DataBind();
materiallistclear();
}
else
{
dr = dt.Rows[rowid];
dr["rmname"] = rmname;
dr["icode"] = icode;
dr["rmcode"] = rmcode;
dr["rmqty"] = qty;
dr["uom"] = uom;
dt.AcceptChanges();
ViewState["TempTable"] = dt;
grdMateriallist.DataSource = (DataTable)ViewState["TempTable"];
grdMateriallist.DataBind();
materiallistclear();
}
To Edit and Delete Link Coding:
protected void grdMateriallist_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
GridViewRow row = (GridViewRow)((Control)e.CommandSource).Parent.Parent;
int RowIndex = row.RowIndex;
lblRowno.Text = RowIndex.ToString();
Label idc = (Label)grdMateriallist.Rows[row.RowIndex].Cells[2].FindControl("rmcode");
DrpMName.SelectedValue = idc.Text;
txtKLQuantity.Text = grdMateriallist.Rows[RowIndex].Cells[3].Text.ToString().Trim();
txtUom.Text = grdMateriallist.Rows[RowIndex].Cells[4].Text.ToString().Trim();
butAdd.Text = "Change";
}
else if (e.CommandName == "Delete")
{
GridViewRow row = (GridViewRow)((Control)e.CommandSource).Parent.Parent;
int RowIndex = row.RowIndex;
DataTable dt = new DataTable();
dt = (DataTable)ViewState["TempTable"];
dt.Rows[RowIndex].Delete();
grdMateriallist.DataSource = dt;
grdMateriallist.DataBind();
materiallistclear();
}
}
protected void grdMateriallist_RowEditing(object sender, GridViewEditEventArgs e)
{
}
protected void grdMateriallist_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
}
After Save Button To Destroy a Viewstate:
private void GrdBranchDesignclear()
{
DataTable dt = new DataTable();
dt.Columns.Add("rmname");
dt.Columns.Add("icode");
dt.Columns.Add("rmcode");
dt.Columns.Add("rmqty");
dt.Columns.Add("uom");
ViewState["TempTable"] = dt;
grdMateriallist.DataSource = "";
grdMateriallist.DataBind();
}
No comments:
Post a Comment