Store ID of RowEditing Instead of Index - c#

In my application, I give the user the option to search the database. When they click edit, I want the GridView to only show the row they are updating by storing its ID.
I'm trying the following code, but it is not returning anything other than the Control.
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
string value = GridView1.Rows[e.NewEditIndex].Cells[3].ToString();
}
Any ideas on how to store the value that I'm looking for?

Use the cell's Text property
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
string value = GridView1.Rows[e.NewEditIndex].Cells[3].Text;
// or
string value = GridView1.Rows[e.NewEditIndex].Cells[3].Value;
}
Or If you have declared the control as <TemplateField> for which you are trying to get the value for then try
string value = ""
Label lblID = (Label)GridView1.Rows[e.NewEditIndex].FindControl("lblID");
value = lblID.Text;
Another alternative would be to add DataKeyNames="ID" for the gridview which you can retrieve using
// assuming that the value of your datakey is numeric value
long Id = long.Parse(GridView1.DataKeys[e.NewEditIndex].Values["ID"].ToString());
Note you can add multiple comma separated values to DataKeyNames property.

Related

How to get Row Id in RepositoryLookupEdit_ValueChanged event

I have gridcontrol that has a RepositoryLookupEdit in one of the columns. I can get the value of RepositoryLookupEdit after changed, but I dont know how to get the which row's RepositoryLookupEdit value changed. How can I get the Row ID?
With the code below, I can get the RepositoryLookupEdit value.
private void repositoryItemLookUpEdit1_EditValueChanged(object sender, EventArgs e)
{
LookUpEdit edit = sender as LookUpEdit;
var row = edit.Properties.GetDataSourceRowByKeyValue(edit.EditValue);
}
Since repositoryItemLookUpEdit isn't restricted to GridControls you cannot get the row handle from this event. You however have other possibilities.
First, if the edit is done by the user, you can use the ColumnView.GetFocusedRow() method to get the current grid row.
If however the edit value is changed via code it will also be changed in the grid so you can now use the ColumnView.CellValueChanged event.
private void repositoryItemLookUpEdit1_EditValueChanged(object sender, EventArgs e)
{
LookUpEdit edit = sender as LookUpEdit;
var row = edit.Properties.GetDataSourceRowByKeyValue(edit.EditValue);
gridRow = gridView.GetFocusedRow() as MyDataRow
}

How to save option selected in Dropdownlist after selection

I am displaying columns in a GridView and one of the columns is a dropdownlist. I want to be able to save the option selected in the dropdownlist as soon as something is selected. I have done this with one of the columns that has a textbox so I was hoping to do something similar with the DropDownList.
The code for the textbox and dropdownlist:
protected void gvPieceDetails_ItemDataBound(object sender, GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.DataRow) {
JobPieceSerialNo SerNo = e.Row.DataItem as JobPieceSerialNo;
if (SerNo != null) {
TextBox txtComment = e.Row.FindControl("txtComment") as TextBox;
txtComment.Text = SerNo.Comment;
txtComment.Attributes.Add("onblur", "UpdateSerialComment(" + SerNo.ID.ToString() + ", this.value);");
DropDownList ddlReasons = (e.Row.FindControl("ddlReasons") as DropDownList);
DataSet dsReasons = DataUtils.GetUnapprovedReasons(Company.Current.CompanyID, "", true, "DBRIEF");
ddlReasons.DataSource = dsReasons;
ddlReasons.DataTextField = "Description";
ddlReasons.DataValueField = "Description";
ddlReasons.DataBind();
ddlReasons.Items.Insert(0, new ListItem("Reason"));
}
}
How to I create an update function for a dropdownlist?
protected void DDLReasons_SelectedIndexChanged(object sender, EventArgs e)
{
string sel = ddlReasons.SelectedValue.ToString();
}
public static void UpdateSerialReason(int SerNoID, string Reasons)
{
JobPieceSerialNo SerNo = new JobPieceSerialNo(SerNoID);
SerNo.Reason = sel; //can't find sel value
SerNo.Update();
}
Dropdownlist:
<asp:DropDownList ID="ddlReasons" runat="server" OnSelectedIndexChanged="DDLReasons_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
I created an OnSelectedIndexChanged function to get the selected value. But how do I then save that value? Is there a way to pass it into the UpdateSerialReason function?
Just move the string sel declaration outside the scope of DDLReasons_SelectedIndexChanged and get the Text of the SelectedItem since it's included in your data source.
private string sel;
protected void DDLReasons_SelectedIndexChanged(object sender, EventArgs e)
{
sel = ddlReasons.SelectedItem.Text;
}
public static void UpdateSerialReason(int SerNoID, string Reasons)
{
JobPieceSerialNo SerNo = new JobPieceSerialNo(SerNoID);
SerNo.Reason = sel; // Should now be available
SerNo.Update();
}
The way you had it previously it was only available in the local scope, i.e, inside the method in which it was being declared and used.
You can get selected value when you call your function:
UpdateSerialReason(/*Some SerNoID*/ 123456, ddlReasons.SelectedValue)
You will lose your value after postback is done if you save value to variable as Equalsk suggested. If you need to use your value on the other page you can save it in session.
If you are working within one asp.net page you can do as I suggested above. Then you can skip the postback on your DropDownList and call UpdateSerialReason when you need :)
And you might want to add property ViewStateMode="Enabled" and EnableViewState="true"

How to capture previous values of columns in a GridView in C#

After updating a row, I am trying to save the previous values of that row.
In other words, I came up with the following code bellow to grab the values of the labels in the grid-view. This code in
gridview1_RowEditing(object sender, GridViewEditEventArgs e)
{
string Name = "";
string day = "";
string Session_name = ((Label)gridview1.Rows[0].FindControl("Name")).Text;
strSession_Name = Session_name;
string Session_type = ((Label)gridview1.Rows[0].FindControl("day")).Text;
strType = Session_type;
}
When edit is clicked the labels turn into drop-down controls where the user can choose a different value. Later, after the user chooses the new value they want, they will click update. This will override the old value. However, when update fires I need somehow to save the old value to a string before it changes to the new one. when the update is running, RowUpdating(object sender, GridViewEditEventArgs e) is executed and the above values are null.
Why don't you do the following
Session["Name"] = ((Label)gridview1.Rows[0].FindControl("Name")).Text;
Session["Day"] = ((Label)gridview1.Rows[0].FindControl("day")).Text;
Then reference the Session["Name"] and Session["Day"] inside RowUpdating

How do I Grab Selected Index on two Dropdownlist's OnSelectedIndexChanged?

I am trying to use two DropDownLists to filter data. I set both of the OnSelectedIndexChanged Equal to the method below. The problem is it is only grabbing the SelectedIndex of the DDL that is changed. Example: if i choose a option in DDL1 it grabs that value and doesnt grab the value of DDL2. They both have the same OnSelectedIndexChanged i figured it would grab the current value of both. Is there a way to make it look at both DDL Controls?
protected void BrandsList_SelectedIndexChanged(object sender, EventArgs e)
{
int DDLcatId = CategoriesList.SelectedIndex;
int DDLBraId = BrandsList.SelectedIndex;
IQueryable<Product> DDLprodResult = GetProductsDDL(DDLcatId, DDLBraId);
if(DDLprodResult == null)
{
}
else
{
CatLab.Text = DDLprodResult.ToList().Count().ToString();
productList.DataSource = DDLprodResult.ToList();
productList.DataBind();
}
}
Your code should work. Of course only one can be changed if you have set AutoPostBack="true"(default is false) on both. But you should get the correct SelectedIndex anyway in the handler.
So i will guess: you are databinding the DropDownLists on every postback. Do this only if(!IsPostBack), otherwise you always overwrite changes with the original values.
So for example in Page_Load:
protected void Page_Load(Object sender, EvengtArgs e)
{
if(!IsPostBack)
{
// DataBind your DropDownLists
}
}

FormView Data Binding

I have for fields in my datasource that I want to combine and display in one label field. I have added a procedure to capture the databinding action but I don't how to get the data out of the datasource. I am displaying this information on a FormView is that makes anly difference. Can I get an example in c#?
For example -
protected void DisplayPayOut(object sender, EventArgs e)
{
Label Payout = FormView1.FindControl("PayoutLabel") as Label;
Payout.Text = datasource.field1 + datasource.field2;
}
I'm not fully sure but it seems like you're looking for something like the following:
protected void DisplayPayOut(object sender, EventArgs e)
{
Label Payout = FormView1.FindControl("PayoutLabel") as Label;
object dataItem = DataBinder.GetDataItem(FormView1);
Payout.Text = DataBinder.Eval(dataItem, "field1NameHere").ToString() + DataBinder.Eval(dataItem, "field2Namehere").ToString();
}

Categories

Resources