I can write a code.
In this I can take a Template Column & in this I build a RadCombobox.
When it's Index changed I want to affect the below text box.
Link the selected value of the Combo box is set as Text on Below TextBox.
Combo Box & Text Box are different Controls of Different Template Column.
I can Write Control of Combo box like this :
<telerik:RadComboBox ID="cmbGID" runat="server" DataSourceID="SqlDataSource8" DataTextField="Name"
DataValueField="ID" AutoPostBack="True" OnSelectedIndexChanged="cmbGID_SelectedIndexChanged">
But I don't know the parameters of this event like this :
protected void cmbGID_SelectedIndexChanged()
{
//code...
}
Any one plz tell me that parameters & tell me is that possible to set txtValue.Text = cmbGID.SelectedValue.ToString()...???
I got the solution of this problem...
This is working by following code :
protected void cmbGrp_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
RadComboBox ddlCtrl = sender as RadComboBox;
GridEditableItem dataItem = ddlCtrl.NamingContainer as GridEditableItem;
RadComboBox cmbCtrl = dataItem.FindControl("cmbSetNo") as RadComboBox;
RadTextBox txtCtrl = dataItem.FindControl("cmbSetNo") as RadTextBox;
txtCtrl.Text = ddlctrl.SelectedValue.ToString();
string query = "QUERY";
ds.Clear();
ds = c.getDataSet(query);
cmbCtrl.DataSource = ds.Tables[0];
cmbCtrl.DataTextField = "NO";
cmbCtrl.DataValueField = "RecordID";
cmbCtrl.DataBind();
}
Something like this should work:
protected void vmbGID_SelectedIndexChanged(object sender, EventArgs e)
{
var ddlCtrl = sender as RadComboBox;
if (ddlCtrl != null)
{
var dataItem = ddlCtrl.Parent as GridDataItem;
if (dataItem != null)
{
var txtCtrl = dataItem.FindControl("txtValue") as RadTextBox;
if (txtCtrl != null)
{
txtCtrl.Text = ddlCtrl.SelectedValue;
}
}
}
}
Related
I have a comboBox with SelectedItemChanged event. So I want to extract value of selected item like:
private void cboCustomerType_SelectedIndexChanged(object sender, EventArgs e)
{
var db = new SQLConnMgr();
ComboBox cmb = (ComboBox)sender;
var comboSelectedValue = cmb.SelectedItem;
}
Problem is value I want is on cmb.SelectedItem.Row.ItemArray[1]
But I can't access to cmb.SelectedItem.Row. Why I can't do as simple as: cmb.SelectedItem.Row.ItemArray[1]? Regards
Your selected item type is DataRowView so you need to cast SelectedItem to that:
private void cboCustomerType_SelectedIndexChanged(object sender, EventArgs e)
{
// A combobox with nothing selected will have a SelectedIndex of -1
if (cboCustomerType.SelectedIndex > -1)
{
// Cast SelectedItem to DataRowView
DataRowView item = cboCustomerType.SelectedItem as DataRowView;
if (item != null)
{
// Access the data in column 1 of the selected row
string value = item[1].ToString();
}
}
}
I believe you are binding combobox with DataSet. So you can do this:
DataRow dataRow = dataSet.Select(string.Format("FieldName = '{0}'", ComboBox.SelectedValue.ToString()))[0];
if (ComboxBox.SelectedValue != null)
{
// can use fields like
String text = dataRow[“fieldName”].ToString();
}
else
{ //something wrong
}
}
Instead of
ComboBox cmb = (ComboBox)sender;
write
ComboBox cmb = sender as ComboBox;
This way it worked for me.
I found a way to add a combobox to DataGridview (Winform) cell, but I have not found an event like ItemDataBound of DataGridView to set a value to comboBox. And do not know how to set a selected value of a comboBox to DataItem property of a current row (of a DataGridView) :(
Please give me some clues to do this task
Thanks you so much
You can use below method to add data to a combobox in gridview. If you dont have a list you can add items to the combobox as:
cmbdgv.Items.Add("Test");
private void bindDataToDataGridViewCombo() {
DataGridViewComboBoxColumn cmbdgv = new DataGridViewComboBoxColumn();
List<String> itemCodeList = new List<String>();
cmbdgv.DataSource = itemCodeList;
cmbdgv.HeaderText = "Test";
cmbdgv.Name = "Test";
cmbdgv.Width = 270;
cmbdgv.Columns.Add(dgvCmbForums);
cmbdgv.Columns["Test"].DisplayIndex = 0;
}
After adding if you want to capture the combobox selection change you can use below event in the datagridview.
ComboBox cbm;
DataGridViewCell currentCell;
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is ComboBox)
{
cbm = (ComboBox)e.Control;
if (cbm != null)
{
cbm.SelectedIndexChanged += new EventHandler(cbm_SelectedIndexChanged);
}
currentCell = this.dataGridView1.CurrentCell;
}
}
void cbm_SelectedIndexChanged(object sender, EventArgs e)
{
this.BeginInvoke(new MethodInvoker(EndEdit));
}
void EndEdit()
{
if (cbm != null)
{
string SelectedItem=cbm.SelectedItem.ToString();
int i = dataGridView1.CurrentRow.Index;
dataGridView1.Rows[i].Cells["Test"].Value = SelectedItem;
}
}
If you are trying to set the value to a Combobox in a DataGridView, see if this answer will help.
To get the selected item of the Combobox (example):
comboBox.SelectedIndexChanged += new EventHandler(comboBox_ComboSelectionChanged);
private void comboBox_ComboSelectionChanged(object sender, EventArgs e)
{
if (myDGV.CurrentCell.ColumnIndex == 5)
{
int selectedIndex;
string selectedItem;
selectedIndex = ((ComboBox)sender).SelectedIndex; // handle an error here.
// get the selected item from the combobox
var combo = sender as ComboBox;
if (selectedIndex == -1)
{
MessageBox.Show("No value has been selected");
}
else
{
// note that SelectedItem may be null
selectedItem = combo.SelectedItem.ToString();
if (selectedItem != null)
{
// Your code
I have a DropDownlist in the GridView, which should be visible only when edit is clicked. I have bound the DropDownList from code behind. When I click on Edit, the label value of that cell should automatically get selected in the DropDownList.
The code I have tried is:
protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
SqlCommand cmd = new SqlCommand("SELECT Location_Name FROM Location_Table");
DropDownList bind_drop = (e.Row.FindControl("DropList") as DropDownList);
bind_drop.DataSource = this.ExecuteQuery(cmd, "SELECT");
bind_drop.DataTextField = "Location_Name";
bind_drop.DataValueField = "Location_Name";
bind_drop.DataBind();
string Loc_type = (e.Row.FindControl("id2") as Label).Text.Trim();
bind_drop.Items.FindByValue(Loc_type).Selected = true;
}
}
When I run the code, it gives an exception error Object reference not set in the last line of the above code.
Cannot find out whats wrong. Kindly help
You must ensure that your list contains label value.
var index = DropDownList1.Items.IndexOf(Loc_type );
if(index > 0)
{
DropDownList1.SelectedIndex = index;
}
else
{
Console.WriteLine("item does not exist");
}
I am using inplace editing on a RadGrid that is built using a class file. Everything is working well except I am having an issue the SelectedIndexChanged event not firing when the grid is in edit mode. Any thoughts?
private void RadGrid_ItemCreated(object sender, GridItemEventArgs e)
{
if (e.Item is GridEditableItem && e.Item.IsInEditMode)
{
try
{
if ((e.Item as GridDataItem) == null) { return; }
((RadNumericTextBox) (e.Item as GridDataItem)["Percentage"].Controls[0]).Width = Unit.Pixel(75);
((TextBox) (e.Item as GridDataItem)["Code"].Controls[0]).Width = Unit.Pixel(75);
RadComboBox _participantList = (e.Item as GridEditableItem)["ID"].Controls[0] as RadComboBox;
if (null == _participantList) { return; }
_participantList.Width = Unit.Pixel(120);
_participantList.DataValueField = "ID";
_participantList.DataTextField = "ID";
_participantList.AutoPostBack = true;
_participantList.DataSource = MAASBaseInterface.ParticipantAPI.GetParticipants();
_participantList.DataBind();
_participantList.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(_participantList_SelectedIndexChanged);
if (!(e.Item.DataItem is GridInsertionObject))
_participantList.SelectedValue = ((Participant) (e.Item.DataItem)).ID.ToString();
if (e.Item.DataItem is GridInsertionObject)
_participantList.EmptyMessage = "-- Select --";
}
catch (Exception ex)
{
string _ex = ex.Message;
}
}
}
void _participantList_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
//first reference the edited grid item through the NamingContainer attribute
GridEditableItem editedItem = (sender as RadComboBox).NamingContainer as GridEditableItem;
int _selectedValue = Convert.ToInt32((editedItem["ID"].Controls[0] as RadComboBox).SelectedValue);
ParticipantList _participants = MAASBaseInterface.ParticipantAPI.GetParticipants();
Participant _participant = _participants.Where(a => a.ID == _selectedValue) as Participant;
RadTextBox _code = editedItem["Code"].Controls[0] as RadTextBox;
_code.ReadOnly = false;
_code.Text = _participant.Code;
}
You need a button that has the CommandName="Select" set. Without that the event doesn't trigger. Could that be the problem?
This link gives more detail
EDIT:
The problem might be that the dropdown list is dynamically added to the grid so that the event needs to be added each time the row is bound. In my experience the radGrid and the GridView works in the same way with respect to the event model so this SO answer might sort you out. Good luck - my initial thoughs were that this couldn't be don't but there may be a way forward
The problem was that I was only setting the Value property of the RadComboBox and not the Text property. Even though text value was showing in the RadComboBox in edit mode apparently it was displaying the Value property. As soon as it was set it started posting back just like it was supposed to do.
if (!(e.Item.DataItem is GridInsertionObject))
{
_participantList.SelectedValue =
((ReinsuranceAgreementParticipant) (e.Item.DataItem)).LegacyReinsurerID.ToString();
// I added this line
_participantList.Text = ((ReinsuranceAgreementParticipant)(e.Item.DataItem)).LegacyReinsurerID.ToString();
}
if (e.Item.DataItem is GridInsertionObject)
_participantList.EmptyMessage = "Select Reinsurer";
I am trying to find a DropDown element in the GridView_RowCommand but it says that GridViewCommandEventArgs does not contain a definituon for 'Row'. I need to do this in this event because i am evaluating a GridView Command. See failing code below
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Add")
{
DropDownList Myddl = null;
ClientClass client = new ClientClass();
Myddl = e.Row.FindControl("ddlClients") as DropDownList;
if (Myddl != null)
{
updated = client.InsertUpdateClient(ClientID,
int.Parse(e.CommandArgument.ToString()), departmentID);
}
else
{
Labels.Text = "There was an error updating this client";
}
}
}
Something like this:
GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
This is assuming what's firing off the RowCommand is a LinkButton. Change that according.
In addition to the #Stephen,
if (e.CommandName == "Add")
{
DropDownList Myddl = null;
ClientClass client = new ClientClass();
//Use this if button type is linkbutton
GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
//Use this if button type is Button
//GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
Myddl = row.FindControl("ddlClients") as DropDownList;
if (Myddl != null)
{
updated = client.InsertUpdateClient(ClientID,
int.Parse(e.CommandArgument.ToString()), departmentID);
}
else
{
Labels.Text = "There was an error updating this client";
}
}