Pass selected row in gridview to drop down list box - c#

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

Related

How to return number of selected rows in datagrid to textbox in WPF?

I would like to select multiple rows (not necessarily in any particular order) and return number of selected rows to the textbox.
Something like:
You have selected 'x' rows.
I was trying to use this kind of method:
private void SelectedRows(object sender, SelectionChangedEventArgs e)
{
var selectedRows = SomeDataGrid.SelectedRows.Count.ToString();
RowCount.Text = "You have selected " + selectedRows + "rows";
}
but to no avail.
There was an error saying that DataGrid do not contain SelectedRow definition.
The DataGrid does not have a SelectedRows property (if you're using Visual Studio, IntelliSense should have warned you about this). Instead, use the property SelectedItems.

get the present value of dropdown when change

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);
}

Change the visible position of ROWs of ultragrid programmatically

I have an ultragrid with an unbound Boolean column that i had named it "Select".
user can select and deselect a row by checked or unchecked the "Select" cell of that row.
but if the number of the grid rows are very much, its difficult for the user to find all of selected rows.
So i want to send the selected rows to the top of the grid just after the user select them.
but i could not found any property that can help me?
I have found my solution myself:
private void ultraGrid1_AfterCellUpdate(object sender, CellEventArgs e)
{
if (e.Cell.Column.Key == "Select" && Convert.ToBoolean(e.Cell.Value))
{
ultraGrid1.Rows.Move(e.Cell.Row, newPosition);
}
}

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

How to get text boxes for Gridview Row values?

Using C# & Mysql
In my webpage am using gridview, if i click the column in the girdview the value should display in the textbox.
For Example
Griview
Column1 column2 Column3
1 Raja 9876
2 Ravi 7890
3 Ramu 9879
...
If i click the 2 rows all the values should display in the textbox
Textbox1.text = 2
textbox2.text = Ravi
textbox3.text = 9879
...,
How to write a code for this condition.
Need C# code Help
I'm assuming that by stating "[...]click the 2 rows[...]" you actually mean "click the 2nd row"; at least, this is what your last snippet suggests, since it shows only the values of the 2nd row (on a little side note: the ID's wrong there; it should be 7890).
The following code snippet shows a GridView which allows the selection of a single row, and uses an event handler in the code-behind to set the text of each TextBox to the according value in the selected row:
Page.aspx:
<asp:GridView runat="server" ID="gridView" OnSelectedIndexChanged="gridview_SelectedIndexChanged" AutoGenerateSelectButton="true"></asp:GridView>
Event handler in the code-behind file Page.aspx.cs:
void gridview_SelectedIndexChanged(object sender, EventArgs e)
{
var grid = sender as GridView;
if (grid == null) return;
//Cell[0] will be the cell with the select button; we don't need that one
Textbox1.Text = grid.SelectedRow.Cell[1].Text /* 2 */;
Textbox2.Text = grid.SelectedRow.Cell[2].Text /* Ravi */;
Textbox3.Text = grid.SelectedRow.Cell[3].Text /* 7890 */;
}
You can use an EditItemTemplate for this.
See the CodeProject article, Editable Gridview with Textbox, CheckBox, Radio Button and DropDown List.

Categories