get the present value of dropdown when change - c#

I have a DropDown that is automatically filled up when another DropDown value is changed:
private void cbWidth_Leave(object sender, EventArgs e)
{
this.InvokeEventHandler(this.AutoFillDropdown);
}
For example: The value of the 1st DropDown is 1 then the 2nd DropDown is also 1.
When I change the value of the 1st DropDown to 2 the 2nd DropDown remains to 1 then change the 1st DropDown to 3 the 2nd DropDown is 2.
The 2nd DropDown only gets the value before change. I need the value of the DropDown after the change so that the 2 DropDown are always similar.

You could do a
ComboBoxEdit1_SelectedValueChanged(object sender, EventArgs e)
{
comboBox2.ValueMember = Convert.ToString(comboBox1.SelectedValue+=1);
}

Related

Conditional Page Break On XtraReports Table

I have a grid on my XtraReports and i would like to place in a conditional page break my list consist of oranges, Lemons and Apples and their descriptions. how do i iterate through each row and get a Field value for particular column.
i have tried accessing the detail Report which is my grid table name and Get Current Column Value but that is not what i am looking for because row 1 will have apple and row 2 will have lemons so i want to break each time when row 1 column 1 does not match row 2 column 1
private void xrPageBreak1_BeforePrint_1(object sender,
System.Drawing.Printing.PrintEventArgs e)
{
SetBreaks(sender);
}
private void SetBreaks(object sender)
{
XRPageBreak control = sender as XRPageBreak;
var ItemName =
DetailReport.GetCurrentColumnValue("Item").ToString();
if(ItemName == "Apple")
{
control.Visible = true;
}
}
the code above returns "Apple"

How to Bind dropdown list from the value of other dropdown list in Gridview

I do have a gridview which has 3 dropdown in it.
for example : if grid view has 15 row than each row has 3 dropdown
1) bind 2nd dropdown using value of 1st dropdown on selected index change of 1st dropdown
2) bind 3rd dropdown using value of 2nd dropdown on selected index change of 2nd dropdown
so how can i achieve that ??
Set AutoPostBack to true on the cascading dropdowns.The handle the SelectedIndexChanged event of the dropdownlist. It's important that you databind the GridView itself not on postbacks, therefore use the IsPostBack property. Otherwise events aren't trigggered.
protected void Ddl1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl1 = (DropDownList)sender;
GridViewRow row = (GridViewRow)ddl1.NamingContainer;
DropDownList ddl2 = (DropDownList)row.FindControl("DropDownList2ID");
// here get the datasource for ddl2 according to ddl1.SelectedValue
ddl2.DataSource = GetDataSource(ddl1.SelectedValue);
ddl2.DataBind();
}
protected void Ddl2_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl2 = (DropDownList)sender;
GridViewRow row = (GridViewRow)ddl2.NamingContainer;
DropDownList ddl3 = (DropDownList)row.FindControl("DropDownList3ID");
// here get the datasource for ddl3 according to ddl1.SelectedValue
ddl3.DataSource = GetDataSource(ddl2.SelectedValue);
ddl3.DataBind();
}

foreach looping only displaying last value of gridview

I'm binding my gridview with multiple datakeys. I'm trying to let these datakey value to be displayed on the textbox when a particular row of gridview is being selected. Therefore i tried the following method below.
I managed to store my database value into a string based on the column number. I'm using a foreach so as to loop the entire gridview when a particular row is being selected. But i'm not very sure why does it only display the last value of my gridview ?
protected void GWCase_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (GridViewRow gvr in GWCase.Rows)
{
string detail = GWCase.DataKeys[gvr.RowIndex].Values[0].ToString();
string propertydetail = GWCase.DataKeys[gvr.RowIndex].Values[1].ToString();
string suspectdetail = GWCase.DataKeys[gvr.RowIndex].Values[2].ToString();
tbdetails.Text = detail;
tbproperty.Text = propertydetail;
tbsuspect.Text = suspectdetail;
}
I cant really convert it into a proper for loop because i got the gridview row there as well. Basically, i'm trying to display out the different value of the different datakey based on the selected gridview row.
Well I think you want value from the selected row only. While looping you are not checking if the row is selected or not. That is why it shows only the stuff from last row. Try following:
protected void GWCase_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow selectedRow = GWCase.SelectedRow;
if(selectedRow == null) return;
/*GET VALUES FROM selectedRow AND DISPLAY THEM*/
}
Answer to your question: Basically, i'm trying to display out the different value of the different datakey based on the selected gridview row.
To get the values from the selected row, Use DataKeyNames property of GridView. Make sure you have a selectbutton present using AutoGenerateSelectButton="true" OR adding a button and set its CommandName Property to "Select".
Set DataKeyNames property to the Primary Key of your table. This can also contain more than one fields.
<asp:GridView ID="Employees" runat="server" DataSourceID="sourceEmployees"
DataKeyNames="EmployeeID">
In your selectedIndexChanged event, you can access the Value of this PrimaryKey column as well as other columns:
protected void gridEmployees_SelectedIndexChanged(object sender, EventArgs e)
{
int index = gridEmployees.SelectedIndex;
//retrieve the Primary Key field from the SelectedDataKey property.
int ID = (int)gridEmployees.SelectedDataKey.Values["EmployeeID"];
// Get other columns values
string firstName = gridEmployees.SelectedRow.Cells[2].Text;
string lastName = gridEmployees.SelectedRow.Cells[1].Text;
lblRegion.Text = gridEmployees.SelectedRow.Cells[9].Text;
}
One another way you can directly determine the data key value of the first key field of the selected row is by using the SelectedValue property
// Returns only the value of First DataKey Field
int ID = (int)gridEmployees.SelectedValue.ToString();
Managed to figure out the answer.
protected void GWCase_SelectedIndexChanged(object sender, EventArgs e)
{
int idx = GWCase.SelectedIndex; //add this line in
string detail = GWCase.DataKeys[idx].Values[0].ToString();
string propertydetail = GWCase.DataKeys[idx].Values[1].ToString();
string suspectdetail = GWCase.DataKeys[idx].Values[2].ToString();
tbdetails.Text = detail;
tbproperty.Text = propertydetail;
tbsuspect.Text = suspectdetail;
}

Pass selected row in gridview to drop down list box

I am trying to pass a selected row in gridview to my dropdown. I have 1 gridview, 1 textbox and 1 dropdown list box. My dropdown is populated by SqlDataSource from my table Country.
On my gridview, I have 3 columns, which are SELECT, NAME and COUNTRY. Under SELECT column, I have my hyperlinked Select, every time I clicked on a certain row NAME will pass in textbox (no problem with that), but in my dropdown it does not populates the COUNTRY that I choose.
For example, I have Jack for name and USA for country. Beside Jack and USA, I have a hyperlink Select, when I click Select, Jack will display on my textbox and supposed to be USA will display on my dropdown but not displaying instead it says an error:
'cboCountry' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value
I have tried the code below:
protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
{
txtname.Text = GridView2.SelectedRow.Cells[1].Text;
cboCountry.Text = GridView2.SelectedRow.Cells[2].Text;
}
Only the textbox is working, no luck at all in dropdown. I'm using C# and asp.net.
Try to use cboCountry.SelectedItem.Text.After getting the value set this thing to dropdown like
protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
{
txtname.Text = GridView2.SelectedRow.Cells[1].Text;
cboCountry.SelectedItem.Text = GridView2.SelectedRow.Cells[2].Text;
}
Hope it works

On edit event how insert a value into the database in the same row as in the gridview?

I have a gridview.
Here are my columns:
The column choose's control is a dropdown with 2 values: yes/no
No Name Choose
1 Meh Yes/No
On edit event I want to insert the selected value of the dropdown in the database of the current edited cell.
protected void GridView1_RowUpdating(Object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = ((GridView)sender).Rows[e.RowIndex];
DropDownList ddl = (DropDownList)row.FindControl("DdlChoose");
bool yes = ddl.SelectedValue == "Yes";
SaveData(params); // pseudo-code
}
1st thing first, i'd suggest data binding, and apply changes to the Database as bulks and not one at a time, unless forced to by definition.
2nd, each row have a key, then it is no problem to execute a Update the Row by
UPDATE mytable SET choose=new_value WHERE key_column=rowkey.
EDIT :
DatagridView Binding to SQL Database example can be found : http://csharp.net-informations.com/datagridview/csharp-datagridview-sql-server.htm

Categories