how to previous selected item remove in dropdown when click on add button in GirdView in Asp.Net - asp.net

How to remove last row selected item in dropdown add button is clicked gridview asp.net.
GridViewRow gvrow = (GridViewRow)((Control)e.CommandSource).NamingContainer;
if (e.CommandName == "Add")
{
ddt = CreateDt();
int j = 0;
foreach (GridViewRow gr in GvPatientAccomDtls.Rows)
{
ddt.Rows.Add();
ddt.Rows[j]["TreatmentCode"] = ((DropDownList)gr.FindControl("ddlTreatment")).SelectedValue;
ddt.Rows[j]["Days"] = ((TextBox)gr.FindControl("txtDays")).Text;
ddt.Rows[j]["Cost"] = ((TextBox)gr.FindControl("txtCost")).Text;
j++;
}
ddt.Rows.Add();
GvPatientAccomDtls.DataSource = ddt;
GvPatientAccomDtls.DataBind();
}

you can handle with RowDataBound Event
void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
var thisRow = (DataRowView)e.Row.DataItem;
var source = thisRow.DataView;
var lastRowIndex = e.Row.DataItemIndex -1;
DataRowView lastRow = null;
var ddl = (DropDownList)e.Item.FindControl("ddl");
DropDownList ddlLast = null;
if(lastRowIndex>=0){
lastRow = source[lastRowIndex];
ddlLast = (DropDownList)((GridView)sender).Rows[lastRowIndex].FindControl("ddl");
//remove the items of this ddl according to the items of the last dll
}
}
}

Related

How to Save gridview values inside inner repeater by onclick in button(grid footer) ? values only that particular grid only?

Click to see that design I need to save on that particular grid values inside inner repeater when I click to save button (inside footer in gridview)
protected void btnAthsave_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
var documentId = btn.CommandArgument;
//Get the Repeater Item reference
//RepeaterItem item = btn.NamingContainer as RepeaterItem;
//GridView grdScopeofwork = (GridView)item.FindControl("grdScopeofwork");
if (rptrMain.Items.Count > 0)
{
for (int i = 0; i < rptrMain.Items.Count; i++)
{
Repeater rptrscope = (Repeater)rptrMain.Items[i].FindControl("rptrscope");
foreach (RepeaterItem repItem in rptrscope.Items)
{
GridView grdScopeofwork = (GridView)repItem.FindControl("grdScopeofwork");
ScopeoftheProjectBOL objBOL = new ScopeoftheProjectBOL();
var fv = grdScopeofwork.FooterRow;
// Button btnsave = ((Button)fv.FindControl("btnsave"));
Button btnsave = fv.Controls[fv.Controls.Count - 1].FindControl("btnsave") as Button;
if (btnsave.CommandName == "ADD")
{
foreach (GridViewRow gvRow in grdScopeofwork.Rows)
{
objBOL.ItemID = Util.ToInt(((Label)gvRow.FindControl("lblID")).Text);
objBOL.ItemCode = ((Label)gvRow.FindControl("lblitem")).Text; ;
objBOL.ProjectID = Util.ToInt(((Label)gvRow.FindControl("lblPJID")).Text);
objBOL.SCID = Util.ToInt(((Label)gvRow.FindControl("lblSCID")).Text);
objBOL.PCID = Util.ToInt(((Label)gvRow.FindControl("lblPCID")).Text);
objBOL.Qty = Util.ToDecimal(((Label)gvRow.FindControl("lblqty")).Text);
objBOL.PricePerUnit = Util.ToDecimal(((Label)gvRow.FindControl("lblRPU")).Text);
DropDownList ddlath = ((DropDownList)gvRow.FindControl("ddlauthor"));
objBOL.Authorization = ddlath.SelectedValue;
objBOL.Status = "Y";
new ScopeoftheProjectBAL().SaveScopeWorkItems(objBOL);
}
}
}
}
}
}
from that image I need to save only PRJ0006-PRD0003-PRD0003 & PRJ0006-PRD0003-PRD0004 items Details only .
i solved the issue...
protected void btnAthsave_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
GridViewRow gvRow1 = (GridViewRow)btn.NamingContainer;
GridView Grid = (GridView)gvRow1.NamingContainer;
RepeaterItem item = Grid.NamingContainer as RepeaterItem;
Repeater rptchild = (Repeater)item.NamingContainer;
RepeaterItem mainitem = rptchild.NamingContainer as RepeaterItem;
Repeater main = (Repeater)mainitem.NamingContainer;
Label rowindex = ((Label)mainitem.FindControl("lblrowindex"));
Label rowindexC = ((Label)item.FindControl("lblrowindexC"));
if (rptrMain.Items.Count > 0)
{
// for (int i = 0; i < rptrMain.Items.Count; i++)
//{
Repeater rptrscope = (Repeater)rptrMain.Items[Util.ToInt( rowindex.Text)].FindControl("rptrscope");
// foreach (RepeaterItem repItem in rptrscope.Items)
// {
GridView grdScopeofwork = (GridView)rptrscope.Items[Util.ToInt("rowindexC")].FindControl("grdScopeofwork");
ScopeoftheProjectBOL objBOL = new ScopeoftheProjectBOL();
GridViewRow fv = grdScopeofwork.FooterRow;
Button btnsave = ((Button)fv.FindControl("btnsave"));
if (btnsave.CommandName == "ADD")
{
foreach (GridViewRow gvRow in grdScopeofwork.Rows)
{
objBOL.ItemID = Util.ToInt(((Label)gvRow.FindControl("lblID")).Text);
objBOL.ItemCode = ((Label)gvRow.FindControl("lblitem")).Text; ;
objBOL.ProjectID = Util.ToInt(((Label)gvRow.FindControl("lblPJID")).Text);
objBOL.SCID = Util.ToInt(((Label)gvRow.FindControl("lblSCID")).Text);
objBOL.PCID = Util.ToInt(((Label)gvRow.FindControl("lblPCID")).Text);
objBOL.Qty = Util.ToDecimal(((Label)gvRow.FindControl("lblqty")).Text);
objBOL.PricePerUnit = Util.ToDecimal(((Label)gvRow.FindControl("lblRPU")).Text);
DropDownList ddlath = ((DropDownList)gvRow.FindControl("ddlauthor"));
objBOL.Authorization = ddlath.SelectedValue;
objBOL.Status = "Y";
new ScopeoftheProjectBAL().SaveScopeWorkItems(objBOL);
}
}
// }
// }
}
}
Add two labels inside the repeters,
<asp:Label runat="server" ID="lblrowindex" Visible="false" Text='<%# Container.ItemIndex %>'></asp:Label>

Dropdown Save in a gridview

I have a GRID that consist of columns that are populated and recently I have added some drop down columns as well to the grid. My drop down is populating with the options from the backend, but now I need to save those answers to the page itself, so the user can see it on the screen next time they arrive on the page.
GRID
protected void gvViolationsCited_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
{
TableCell idCell = GridViewFunctions.FindCellByDataField(e.Row, "ID");
idCell.Visible = false;
if (e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton lb = (ImageButton)e.Row.FindControl("lbShowModalDialog");
long id = long.Parse(idCell.Text);
DropDownList hpvcriteria = (DropDownList)e.Row.FindControl("hpvcriteria");
hpvcriteria.DataSource = LookupDataLoaderService.Instance.LoadLookupData(LookupTables.HPV_VIOLATION_CODE);
hpvcriteria.DataBind();
}
}
}
private void PopulateViolationGrid(long caseID)
{
IList<EnforcementViolation> enforcementViolations = LovService.Instance.GetEnforcementLovList(caseID);
IEnumerator<EnforcementViolation> evEnumerator = enforcementViolations.GetEnumerator();
while (evEnumerator.MoveNext())
{
EnforcementViolation ev = evEnumerator.Current;
//txtOtherDocsViolated.Text += ev.Permit + " ";
}
gvViolationsCited.DataSource = GridViewFunctions.GenericListToDataTable<EnforcementViolation>(enforcementViolations);
gvViolationsCited.DataBind();
upnlViolationsCited.Update();
evEnumerator.Dispose();
}

how to add footer template dynamically in asp.net

I have created a gridview dynamically. There is template fields described in designing portion. All the columns were created thru code behind as follows. Its works fine. Here I can listed the pages for each rows. But I dont know how to implement the sum of pages in the footer template thru code behind.
TemplateField Pages = new TemplateField();
Pages.HeaderText = "Pages";
Pages.ItemTemplate = new GridViewTemplate_Pages();
gv1.Columns.Add(Pages);
public class GridViewTemplate_Pages : ITemplate
{
void ITemplate.InstantiateIn(Control container)
{
Label PagesLabel = new Label();
PagesLabel.DataBinding += new EventHandler(this.PagesLabel_DataBinding);
container.Controls.Add(PagesLabel);
}
void PagesLabel_DataBinding(object sender, EventArgs e)
{
Label lbl1 = (Label)sender;
GridViewRow row = (GridViewRow)lbl1.NamingContainer;
lbl1.Text = DataBinder.Eval(row.DataItem, "PagesReceived").ToString();
}
}
Given ShowFooter="True" in aspx page and RowDataBound written separately. The following code works fine if I given footer template in aspx page but do not know how to get the result in programmatically. Please advice.
protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int RowTotalPages = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "PagesReceived"));
TotalPages = TotalPages + RowTotalPages;
}
if (e.Row.RowType == DataControlRowType.Footer)
{
Label m = (Label)e.Row.FindControl("gv1TotalPages");
m.Text = TotalPages.ToString();
}
}
You can create the footer to a gridview like this.
//Code
GridView gv = new GridView();
gv.RowCreated += delegate(object dsender, GridViewRowEventArgs ge)
{
if (ge.Row.RowType == DataControlRowType.Footer)
ge.Row.Cells[0].Text = "Something";
};
gv.AutoGenerateColumns = false;
gv.ShowFooter = true;
BoundField bf = new BoundField();
bf.HeaderText = "col 1";
bf.DataField = "Length";
gv.Columns.Add(bf);
gv.DataSource = new string[] { "One", "Two", "Three" };
gv.DataBind();
Form.Controls.Add(gv);
This is for the dynamically created gridview and a footer. You can change accordingly.
Its a silly thing. I made it complex. I need the sum of pages in the footer row, then why should I think and confuse about GridViewTemplate and Itemplate class. Simply I added the Total pages in the session and recollect it in the DataControlRowType.Footer. Here is the code:
protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int RowTotalPages = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "PagesReceived"));
int RowTotalCharges = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "ChargesReceived"));
TotalPages = TotalPages + RowTotalPages;
TotalCharges = TotalCharges + RowTotalCharges;
Session.Add("TotalPages", TotalPages.ToString());
Session.Add("TotalCharges", TotalCharges.ToString());
}
if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[4].Text = "Batches Count: " + gv1.Rows.Count.ToString();
e.Row.Cells[5].Text = Session["TotalPages"].ToString();
e.Row.Cells[6].Text = Session["TotalCharges"].ToString();
}
}

Prepopulate gridview empty datasource

I use an gridview empty datasource for bulk insert, how would I prepopulate for instance Column A with the value of a drop down box? So far I have below, but its not working
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox tb = (TextBox)e.Row.FindControl("YourTextBoxID");
if(tb != null)
{
tb.Text = DropDownList1.SelectedItem.Text;
}
}
}
I don't understand what you mean with empty DataSource. I assume that you actully mean a DataSource which is populated automatically with a default value.
You could use a DataTable:
var tbl = new DataTable();
tbl.Columns.Add("ColumnA");
var defText = DropDownList1.SelectedItem.Text;
for(int i = 0; i < 1000; i++)
{
tbl.Rows.Add(defText);
}
GridView1.DataSource = tbl;
GridView1.DataBind();

asp.net - GridView dynamic footer row creation problem

I am able to create BoundFields and Footer-rows dynamically like this in my GridView:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
CreateGridView();
}
}
private void CreateGridView()
{
GridView1.Columns.Clear();
DataTable dataTable = Book.GetBooksDataSet().Tables[0];
CommandField cf = new CommandField();
cf.ShowEditButton = true;
GridView1.Columns.Add(cf);
int colCount = 1;
foreach (DataColumn c in dataTable.Columns)
{
BoundField boundField = new BoundField();
boundField.DataField = c.ColumnName;
boundField.HeaderText = c.ColumnName;
//boundField.FooterText = "---";
if (colCount == 3 || colCount == 5)
{
boundField.ReadOnly = true;
}
GridView1.Columns.Add(boundField);
colCount++;
}
GridView1.ShowFooter = true;
GridView1.DataSource = dataTable;
GridView1.DataBind();
GridViewRow footerRow = GridView1.FooterRow;
Button b = new Button();
b.Text = "Add New";
int i = 0;
footerRow.Cells[i].Controls.Add(b);
foreach (DataColumn c in dataTable.Columns)
{
++i;
TextBox tb = new TextBox();
footerRow.Cells[i].Controls.Add(tb);
}
}
....................................
....................................
....................................
}
But the problem is, when I click the "Add New" - button, it disappears instantly. And, also I am unable to add any event handler to it. Or intercept its actions like this:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
if (e.CommandName == "Edit")
{
GridView1.EditIndex = index;
GridViewRow selectedRow = ((GridView)e.CommandSource).Rows[index];
//We can get cell data like this
string id = selectedRow.Cells[1].Text;
string isbn = selectedRow.Cells[2].Text;
//This is necessary to GridView to be showed up.
CreateGridView();
}
else if (e.CommandName == "Update")
{
LinkButton updateButton = (LinkButton)e.CommandSource;
DataControlFieldCell dcfc = (DataControlFieldCell)updateButton.Parent;
GridViewRow gvr = (GridViewRow)dcfc.Parent;
//The update...................
//Update grid-data to database
UpdateDataInTheDatabase(gvr.Cells[1].Controls);
//Grid goes back to normal
GridView1.EditIndex = -1;
//This is necessary to GridView to be showed up.
CreateGridView();
}
}
One more thing, I have seen some solutions that suggests to handle the GridView's rowBound event. But I need to do it from within Page_load event handler, or in, GridView1_RowCommand event handler.
Dynamically created controls mus be re-created on every postback. Your "Add New" button causes a postback so the dynamically created footer disappears. Is there a reason this grid has to be created dynamically? From the code you posted it appears that you could do this in markup instead. If not, you'll have to re-create the dynamic controls on every postback.
Edited to add:
I played with this a little bit and what's below works in that the grid doesn't disappear and events are handled, but it doesn't actually do anything. Hope this helps.
Markup:
<p><asp:Literal ID="Literal1" runat="server" /></p>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
OnRowCommand="GridView1_RowCommand"
OnRowEditing="GridView1_RowEditing"/>
Code:
protected void Page_Load(object sender, EventArgs e)
{
BindGridView();
}
private DataTable GetBooksDataTable()
{
var dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Title", typeof(string));
dt.Columns.Add("Author", typeof(string));
for (int index = 0; index < 10; index++)
{
dt.Rows.Add(index, "Title" + index, "Author" + index);
}
return dt;
}
private void BindGridView()
{
var dt = GetBooksDataTable();
GridView1.Columns.Clear();
GridView1.ShowFooter = true;
var cf = new CommandField();
cf.HeaderText = "Action";
cf.ShowEditButton = true;
GridView1.Columns.Add(cf);
for (int index = 0; index < dt.Columns.Count; index++)
{
var boundField = new BoundField();
boundField.DataField = dt.Columns[index].ColumnName;
boundField.HeaderText = dt.Columns[index].ColumnName;
GridView1.Columns.Add(boundField);
}
GridView1.DataSource = dt;
GridView1.DataBind();
var footer = GridView1.FooterRow;
var b = new LinkButton();
b.Text = "Add New";
b.CommandName = "Add New";
footer.Cells[0].Controls.Add(b);
for (int index = 1; index < dt.Columns.Count + 1; index++)
{
var tb = new TextBox();
footer.Cells[index].Controls.Add(tb);
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
Literal1.Text = e.CommandName;
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
Literal1.Text = "Editing row index " + e.NewEditIndex.ToString();
}
Move your code from Page_Load to Page_Init. Things added in the Page_Load last only for the lifecycle of one postback.
You'd then be able to add eventhandlers, intercept events etc.

Resources