How do I find a GridView item at button_click? - asp.net

I am using the following code:
foreach (GridViewRow dr in gvrejectreq.Rows)
{
DropDownList ddl=(DropDownList)gvrejectreq.Rows[dr.RowIndex].FindControl("DropDownList1");
string status = ddl.SelectedValue;
int userid = gvrejectreq.Rows[dr.RowIndex].
}

If I understand what you're asking...you don't need to loop...
protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e){
if (e.CommandName == "thiscommandname") {
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow selectedRow = ((GridView)e.CommandSource).Rows[index];
//and then for example...
string rowText = (LinkButton)selectedRow.Cells[0].Controls[0]).Text;}
}

Related

Cant Get Data To Return To My Grid View Using Linq in ASP.NET

Not sure why but i cant get my data to return. It starts out saying there is not data records to display. But when i select the month and year of the information i am looking for the grid view disappears. Any suggestions
NutritionEntities context;
protected void Page_Load(object sender, EventArgs e)
{
context = new NutritionEntities();
if (!IsPostBack)
{
for (int i = DateTime.Now.Year; i > 1988; i--)
{
ddlYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
}
int selectedMonth = int.Parse(ddlMonth.SelectedValue);
int selectedYear = int.Parse(ddlYear.SelectedValue);
GridView1.DataSource =
from exercise in context.Exercises
where exercise.exerciseDate.Value.Month == selectedMonth && exercise.exerciseDate.Value.Year == selectedYear
select new
{
exercise.exerciseDate,
exercise.duration,
exercise.caloriesBurned,
exercise.averageHR,
exercise.distanceInMiles,
exercise.notes
};
GridView1.DataBind();
}
}
protected void ddlMonth_SelectedIndexChanged(object sender, EventArgs e)
{
int selectedMonth = int.Parse(ddlMonth.SelectedValue);
int selectedYear = int.Parse(ddlYear.SelectedValue);
GridView1.DataSource =
from exercise in context.Exercises
where exercise.exerciseDate.Value.Month == selectedMonth && exercise.exerciseDate.Value.Year == selectedYear
select new
{
exercise.exerciseDate,
exercise.duration,
exercise.caloriesBurned,
exercise.averageHR,
exercise.distanceInMiles,
exercise.notes
};
GridView1.DataBind();
}
protected void ddlYear_SelectedIndexChanged(object sender, EventArgs e)
{
int selectedMonth = int.Parse(ddlMonth.SelectedValue);
int selectedYear = int.Parse(ddlYear.SelectedValue);
GridView1.DataSource =
from exercise in context.Exercises
where exercise.exerciseDate.Value.Month == selectedMonth && exercise.exerciseDate.Value.Year == selectedYear
select new
{
exercise.exerciseDate,
exercise.duration,
exercise.caloriesBurned,
exercise.averageHR,
exercise.distanceInMiles,
exercise.notes
};
GridView1.DataBind();
}
you have verify the binding of your control grid, i suggest add only object to the datasource of the grid on the pageLoad when the postback its false like this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource =
from exercise in context.Exercises
where exercise.exerciseDate.Value.Month == selectedMonth && exercise.exerciseDate.Value.Year == selectedYear
select new
{
exercise.exerciseDate,
exercise.duration,
exercise.caloriesBurned,
exercise.averageHR,
exercise.distanceInMiles,
exercise.notes
};
}
GridView1.DataBind();
}
the problem is when you add the objects to datasource , when any event refresh the web page like select the control is unbinding, i hope help you,, see you!!

Extract index(row) from a textbox in a gridview in asp.net

Hi i want to extract index (Rows) from a grid view and i use a text box in it. the id should extract from text box in gridview. I use this code:
protected void TextBox_Email_TextChanged(object sender, EventArgs e)
{
TextBox thisTextBox = (TextBox)sender;
GridViewRow currentRow = (GridViewRow)thisTextBox.Parent.Parent;
int rowindex = 0;
int idx = currentRow.RowIndex;
string ArticleID = (string)GridView1.DataKeys[idx].Values[1];
}
But the VS gave me this error:
Unable to cast object of type 'System.Web.UI.WebControls.DataControlFieldCell' to type 'System.Web.UI.WebControls.GridViewRow'.
Can any body helps me?
Thanks
The parent of the DataControlFieldCell will be the GridViewRow:
protected void TextBox_Email_TextChanged(object sender, EventArgs e)
{
TextBox thisTextBox = (TextBox)sender;
GridViewRow currentRow = (GridViewRow)thisTextBox.Parent.Parent.Parent;
int rowindex = 0;
int idx = currentRow.RowIndex;
string ArticleID = (string)GridView1.DataKeys[idx].Values[1];
}

Gridview findcontrol after dropdownlist event

I want to findcontrol on griview after DDL OnSelectedIndexChanged event. where the target control is on the rowindex where the DDL is located..
here my codes;
protected void Page_Load(object sender, EventArgs e)
{
ArrayList Dummysource = new ArrayList() { "AA", "BB", "CC", "DD" };
if(!IsPostBack )
{
GridView1.DataSource = Dummysource;
GridView1.DataBind();
}
}
protected void ddlsample_OnSelectedIndexChanged(object sender, EventArgs e)
{
string valueComponent = (sender as DropDownList).SelectedItem.Value;
Label1.Text = valueComponent;
}
int ddlvalue;
protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
//Checking whether the Row is Data Row
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Finding the Dropdown control.
DropDownList ddlsample = (DropDownList)e.Row.FindControl("ddlsample");
Label ilbldata = (Label)e.Row.FindControl("lbldata");
if (ddlsample != null)
{
switch(ilbldata.Text)
{
case "AA":
ddlvalue = 2;
break;
case "BB":
ddlvalue = 3;
break;
case "CC":
ddlvalue = 4;
break;
case "DD":
ddlvalue = 5;
break;
}
for (int i = 1; i <= ddlvalue; i++ )
{
ddlsample.Items.Add(i.ToString() );
}
}
}
}
protected void GridView1_SelectedIndexChanged(Object sender, EventArgs e)
{
GridView gv = sender as GridView;
gv = GridView1;
Label foo = gv.SelectedRow.FindControl("lbldata") as Label ;
Label2.Text = foo.Text;
}
the code get the value of the DropDownList selected Item. I'm Wondering on how to get the component value in the gridview. after selectedindexchange event of DDL
I made some visual photo for more clear
http://i1288.photobucket.com/albums/b493/Kasparov1/GridviewDDL_zps3721fb97.png
thanks in advance;
Try this
protected void ddlsample_OnSelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
Label1.Text = ddl.SelectedItem.Value;
GridViewRow row = (GridViewRow)ddl.NamingContainer;
// Find your control
Control control = row.FindControl("myControl");
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
DropDownList drop = GridView1.Controls[0].Controls[0].FindControl("DropDownList1") as DropDownList;
string text = drop.Items[drop.SelectedIndex].ToString();
//Find FooterRow Control
DropDownList dT = GridView1.FooterRow.FindControl("DropDownList1") as DropDownList;
string text = dT.Items[dT.SelectedIndex].ToString();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)//DropDownList1 in GridVied
{
//Find FooterRow Control
DropDownList drop = GridView1.FooterRow.FindControl("DropDownList1") as DropDownList;
string text = drop.Items[drop.SelectedIndex].ToString();
//find normal DropDownList1
DropDownList drop1 = GridView1.FindControl("DropDownList1") as DropDownList;
string text = drop1.Items[drop1.SelectedIndex].ToString();
}
//ADD list in GRIDVIEW dropdownlist at run time
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1");//Gridview DropDownList
ddl.Items.Add("- - Select - -");
ddl.Items.Add(new ListItem("ABCD"));
ddl.Items.Add(new ListItem("EFGH"));
}

Converting Gridview to RadGrid

How do I change the below code from GridView to RadGrid? Below is the code for my gridview:
protected void gv_Movie_RowCommand(object sender, GridViewCommandEventArgs e)
{
//get the row number of the selected row
int rowNo = int.Parse(e.CommandArgument.ToString());
//get the selected row
GridViewRow row = gv_Movie.Rows[rowNo];
//Get movie ID, which is on the 1st column of the gridview
string movieID = row.Cells[0].Text;
if (e.CommandName == "Select")
{
Response.Redirect("MovieSelect.aspx?id=" + movieID);
}
else if (e.CommandName == "Update")
{
Response.Redirect("MovieUpdate.aspx?id=" + movieID);
}
}
I tried the below code and it doesn't work at all due to e.CommandArgument. Any solution to this?
protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
int rowNo = int.Parse(e.CommandArgument.ToString());
GridDataItem row = RadGrid1.Items[rowNo];
string movieID = row.Cells[0].Text;
if (e.CommandName == "Select")
{
Response.Redirect("movieSelect.aspx?id=" + movieID);
}
else if (e.CommandName == "Delete")
{
Response.Redirect("movieUpdate.aspx?id=" + movieID);
}
}
instead of
int rowNo = int.Parse(e.CommandArgument.ToString());
GridDataItem row = RadGrid1.Items[rowNo];
use
GridDataItem row = e.Item as GridDataItem;
Telerik automatically retrieves current row as e.Item. The rest should work the same.
Or better yet, if appropriate, try to Utilize DataKeyNames.
So in your markup, you will have something like:
<telerik:RadGrid id="grid" runat="server">
<MasterTableView DataKeyNames="movieID">
.....
</MasterTableView>
</telerik:RadGrid>
Then, you can retireve movieID like this:
var row = e.Item as GridDataItem;
string movieID = row.GetDataKeyValue("movieID");

Edit,delete,update operations in SPgridview in sharepoint 2010 using Webparts

I have a list in my SharePoint site with name "Empdetails" and having columns (EmpName string, Empaddress string).
I have to bind the list data to the SpGridview with edit, delete, update functionality.
I am able to bind the list data to gridview successfully, but I am unable to provide edit, delete, update functionality to the gridview.
Code:
private void binddata()
{
SPWeb mySite = SPContext.Current.Web;
SPList myList = mySite.Lists["Empdetails"];
SPListItemCollection items = myList.Items;
//Here we will make a datatable and we will put our list data to the data table
DataTable table=new DataTable();
table.Columns.Add("EmpName", typeof(string));
table.Columns.Add("Empaddress", typeof(string));
// Create rows for each splistitem
DataRow row;
foreach (SPListItem result in items)
{
row = table.Rows.Add();
row["EmpName"] = result["EmpName"].ToString();
row["Empaddress"] = result["Empaddress"].ToString();
}
//binding data to gridview
GridView1.DataSource = table.DefaultView;
GridView1.DataBind();
}
You will need to write all of the code for updates and deletes. It is not provided automatically.
Personally, I would recommend using the out of the box List View web part that points to a Datasheet view rather than trying to create my own.
But if you must write custom code, your code above might be able to be simplified from this:
DataTable table = new DataTable();
table.Columns.Add("EmpName", typeof(string));
table.Columns.Add("Empaddress", typeof(string));
DataRow row;
foreach (SPListItem result in items)
{
row = table.Rows.Add();
row["EmpName"] = result["EmpName"].ToString();
row["Empaddress"] = result["Empaddress"].ToString();
}
to this:
DataTable table = items.GetDataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bindata();
}
}
public void Bindata()
{
SPWeb web = SPContext.Current.Web;
SPList list = web.Lists["VisualWebpart"];
DataTable dt = new DataTable();
dt.Columns.Add("Title", typeof(string));
foreach (SPListItem item in list.Items)
{
DataRow dr = dt.NewRow();
dr["Title"] = item["Title"].ToString();
dt.Rows.Add(dr);
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void grd_Insert(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Insert")
{
SPWeb currentWeb = SPContext.Current.Web;
SPList lst = currentWeb.Lists["VisualWebpart"];
SPListItemCollection myColl = lst.Items;
TextBox txtTitle = (TextBox)GridView1.FooterRow.FindControl("txtTitle");
SPListItem item = myColl.Add();
item["Title"] = txtTitle.Text;
item.Update();
txtTitle.Text = "";
Bindata();
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
Bindata();
}
protected void GridView1_Cancel(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
Bindata();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
int Title = row.DataItemIndex;
SPWeb currentWeb = SPContext.Current.Web;
SPList lst = currentWeb.Lists["VisualWebpart"];
SPListItemCollection myColl = lst.Items;
int itemcount = myColl.Count;
for (int i = 0; i <= itemcount-1; i++)
{
SPListItem item = myColl[i];
if (Title==i)
{
myColl.Delete(i);
}
}
Bindata();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
int Title = row.DataItemIndex;
SPWeb currentWeb = SPContext.Current.Web;
SPList lst = currentWeb.Lists["VisualWebpart"];
SPListItemCollection myColl = lst.Items;
int itemcount = myColl.Count;
TextBox txtTit = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtTit");
string d = txtTit.Text;
for (int i = 0; i <= itemcount - 1; i++)
{
SPListItem item = myColl[i];
if (Title == i)
{
item["Title"] = d;
item.Update();
}
}
GridView1.EditIndex = -1;
Bindata();
}
}

Resources