I have a datagridview with lots of data, and when I add a new line, the first column's last row creates a new ComboBoxCell which contain four items. But I can't set the default value ("DropDown") for the combobox. Every time I must manually select "DropDown". What is the solution?
DataGridViewComboBoxCell dgvCell = new DataGridViewComboBoxCell();
dgv[1, dgv.Rows.Count - 1] = dgvCell;
string[] controltype = {"DropDown", "CheckBoxList", "ListControl", "Tree" };
dgvCell.DataSource = controltype;
private void dataGridView_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
{
e.Row.Cells[4].Value = "DropDown";
}
it,s easy,If you have a ComboBox Column in your DataGrid View and you want to know what is the selected index of the combo box, then you need to do this:
1. Handle the EditingControlShowing event of DataGrid View. In this event handler, check if the current column is of our interest. Then we create a temporary ComboBox object and get the selected index:
Code
private void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex == 0)
{
// Check box column
ComboBox comboBox = e.Control as ComboBox;
comboBox.SelectedIndexChanged += new EventHandler(comboBox_SelectedIndexChanged);
}
}
void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
int selectedIndex = ((ComboBox)sender).SelectedIndex;
MessageBox.Show("Selected Index = " + selectedIndex);
}
try :
if(!isPostBack)
{
dgvCell.SelectedItem=controltype[0].toString();
}
Related
Hi I'm trying to grab the second column value on click and to display in a text box but im really new to C#.
private void DataGrid_SelectionChanged(object sender, SelectionChangeEventArgs e)
{
display.Text = Datagrid.SelectedItem.ToString();
}
This displays all columns, but i only want second column, the second colummn header is Name.
var query = from loan in Loans
select new {Date = loan.StatusCommittedDate, Name = loan.PublicationName}
DataGrid.ItemsSource = query.ToList();
Based on how your datagrid is binded this may work
private void DataGrid_SelectionChanged(object sender, SelectionChangeEventArgs e)
{
DataGrid dataGrid = sender as DataGrid;
int selectedIndex = dataGrid.SelectedIndex;
if (selectedIndex > -1)
{
DataGridColumn column = dataGrid.Columns[0];
Label lblName = (Label)column.FindControl("ControloftheIDwithPublicationNameBinded");
display.text = lblName.text;
}
}
Hope this helps
I have a System.Windows.Forms.DataGridView in which one column has only ComboBoxes. When something changes in a ComboBox, I need to know what the new item is, and the row index of the ComboBox in which the event took place. The latter is giving me trouble. I have the following:
class MyForm : Form
{
private System.Windows.Forms.DataGridView m_GridView;
private System.Windows.Forms.DataGridViewComboBoxColumn m_ComboBoxColumn;
public MyForm ()
{
/* ... lots of initialisation stuf...*/
this.m_GridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.m_ComboBoxColumn});
}
private void m_GridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox comboBox = e.Control as ComboBox;
if (comboBox != null)
{
comboBox.SelectedIndexChanged -= new EventHandler(m_ComboBoxColumn_SelectedIndexChanged);
comboBox.SelectedIndexChanged += new EventHandler(m_ComboBoxColumn_SelectedIndexChanged);
}
}
private void m_ComboBoxColumn_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox comboBox = (ComboBox) sender;
string item = comboBox.Text;
if (item != null)
{
System.Diagnostics.Debug.WriteLine(item); // This is the new item text
// Now I need the row index of this ComboBox in 'm_GridView' (or 'm_ComboBoxColumn')
}
}
}
How can I find the row index of the ComboBox in the DataGridViewComboBoxColumn in this last method?
You should cast it to DataGridViewComboBoxEditingControl and access the EditingControlRowIndex to get the row index like this:
var comboBox = (DataGridViewComboBoxEditingControl)sender;
int rowIndex = comboBox.EditingControlRowIndex;
You can get it via the ComboBox's SelectedIndex property - http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedindex.aspx
var index = comboBox.SelectedIndex;
I have a combobox(CB1) and it contains items like 1,2,3 and i want to make another combobox (CB2) visible when i select the value 3 from CB1. Which property should i user. I am working on a windows based application and I am using C# as the code behind language. An example would be great to solve the problem.
The combo box CBFormat consists of a list of items as follows:
var allWiegandFormat = WiegandConfigManager.RetrieveAllWiegandFormats();
var allWiegandList = new List<IWiegand>(allWiegandFormat);
CBFormat.Items.Add(allWiegandList[0].Id);
CBFormat.Items.Add(allWiegandList[3].Id);
CBFormat.Items.Add(allWiegandList[4].Id);
CBFormat.Items.Add(allWiegandList[5].Id);
CBProxCardMode.Items.Add(ProxCardMode.Three);
CBProxCardMode.Items.Add(ProxCardMode.Five);
Now I want to show the Combo box of CBPorxCardMode when i select the second item from CBFormat combo box.
Try this
Private void CB1_SelectedIndexChanged(object sender, System.EventArgs e)
{
Combobox CB = (ComboBox) sender;
if(CB.SelectedIndex != -1)
{
int x = Convert.ToInt32(CB.Text)
if(x == 3)
{
CB2.Visible = True;
}
}
}
Use SelectionChangeCommitted event and subscribe your CB1 to it:
// In form load or form initialization
cb1.SelectionChangeCommitted += ComboBoxSelectionChangeCommitted;
// Event
private void ComboBoxSelectionChangeCommitted(object sender, EventArgs e)
{
cb2.Visible = cb1.SelectedItem != null && cb1.Text == "3";
}
If it is Winforms You can use Something Like this
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.Add(1);
comboBox1.Items.Add(2);
comboBox1.Items.Add(3);
comboBox2.Visible = false;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedItem.ToString() == "3")
{
comboBox2.Visible = true;
}
else
{
comboBox2.Visible = false;
}
}
Hope this helps.,
Start with the CB2 Visible property set to False and add a event handler code for the SelectedIndexChanged on the CB1 through the WinForms designer
private void ComboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
ComboBox comboBox = (ComboBox) sender;
if(comboBox.SelectedItem != null)
{
int id = Convert.ToInt32(comboBox.SelectedItem)
cbo2.Visible = (id == 3)
}
}
This is supposing the ID, that you are adding at the first combo, is an Integer value as it seems.
Also rembember that SelectedIndexChanged event will be called even if you change the SelectedItem programmatically and not just when the user changes the value. Also, if the user change again the selection moving away from the ID==3 the method will set again the Cbo2 not visible.
I am adding ComboBoxes dynamically at runtime as shown below.
The problem that I am having is that i do not know which of the comboboxes the user is using.
For eg. The user decides to add 5 comboBoxes to the form, and then goes to the first comboBox,and selects a value, I need to retrieve the value of that comboBox.
What the below code is doing - My approach
I am adding a comboBox to a FlowlayoutPanel and the retrieve its name based on the mouse co-ordinates.... this by the way is not working... and I have no idea what to do.
Any help is greatly appreciated.
public partial class Form1 : Form
{
int count = 0;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
count += 1;
ComboBox cb = new ComboBox();
cb.Name = count.ToString();
cb.MouseHover += new EventHandler(doStuff);
Label lb = new Label();
lb.Text = count.ToString();
flowLayoutPanel1.Controls.Add(cb);
flowLayoutPanel1.Controls.Add(lb);
}
public void doStuff(object sender, EventArgs e)
{
label1.Text = flowLayoutPanel1.GetChildAtPoint(Cursor.Position).Name;
}
}
}
You could try:
cb.SelectionChangeCommitted += selectionChangedHandler
...
void selectionChangedHandler(object sender, EventArgs e) {
ComboBox cb = (ComboBox)sender;
label1.Text = cb.Name;
// Do whatever else is needed with the combo box
}
The SelectionChangeCommitted event is "raised only when the user changes the combo box selection", which sounds like what you're after.
The combobox that raised the event in your doStuff-eventhandler is in the sender-parameter. Try casting it to a checkbox likte this:
ComboBox boxThatRaisedTheEvent = (ComboBox)sender;
string text = ((ComboBox)this.GetChildAtPoint(pt)).Text;
public void DoStuff(object sender, EventArgs e)
{
var comboBox = sender as ComboBox;
var name = (comboBox != null ? comboBox.Name : null);
}
this code casts the 'sender' parameter to a ComboBox object and if the cast is done correctly assings the ComboBox name to the string 'name', otherwise 'name' is null.
Tip: The C# coding style suggests that method names should start with capitalized letter.
You can try something like:
flowLayoutPanel1.Controls.OfType<ComboBox>().FirstOrDefault(cb => cb.Name.Equals(NAME_OF_COMBOBOX))
Or better:
ComboBox box = (ComboBox)sender;
I have a datagrid with 2 combobox columns. I wrote selection changed event for the combobox column as follows.
private void Grid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox cb = e.Control as ComboBox;
if (cb!=null)
{ cb.SelectionChangeCommitted -= new EventHandler(cb_SelectedIndexChanged);
// now attach the event handler
cb.SelectionChangeCommitted += new EventHandler(cb_SelectedIndexChanged);
}
}
void cb_SelectedIndexChanged(object sender, EventArgs e)
{
var tb = datagrdADDTEMP.EditingControl as ComboBox;
if (tb != null)
str = tb.SelectedValue != null ? tb.SelectedValue.ToString() : null;
Assesment_Business_layer.Businesslayer bl = new Assesment_Business_layer.Businesslayer();
DataSet ds = new DataSet();**strong text**
ds = bl.GetSubCatNamesBA(str);
cmbDataGridSubCategory.DataSource = ds.Tables[0];
cmbDataGridSubCategory.DisplayMember = "SubCategoryName";
cmbDataGridSubCategory.ValueMember = "SubCategoryCode";
}
}
its working well with the first combobox column but the problem is the above selection changed event is also raising when i am selecting the item from the second combobox column..but i dont want to raise the selection changed event for the second combos column. It should raise only for the first combobox only.
Please help as l'm stuck up with this problem.
The problem seems that you're adding the event handler to any combo box, doesn't matter what column it is, so you must find first in what column the event was triggered, for this you must take a look at the sender object of the Grid_EditingControlShowing event handler (which is a DataGridView) and its CurrentCell, SelectedColumns or SelectedCells properties.
Example:
private void Grid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if(((DataGridView)sender).CurrentCell.ColumnIndex == 0) //Assuming 0 is the index of the ComboBox Column you want to show
{
ComboBox cb = e.Control as ComboBox;
if (cb!=null)
{
cb.SelectionChangeCommitted -= new EventHandler(cb_SelectedIndexChanged);
// now attach the event handler
cb.SelectionChangeCommitted += new EventHandler(cb_SelectedIndexChanged);
}
}
}
An example using SelectedColumns or SelectedCells, will be pretty much like this, if you want more info about that properties you can take a look at their documentation on MSDN