i am new in programming
i have 6 drop down in one page 1 want them to make them read only (non editable)
if value is selected
and rest of the drop down should not be read only if value is selected
is there any way i can check all the Drop down in one condition/check(at one go)
thanks in advance
1# Find all dropdownlists
Dim allDDs = From dd As DropDownList In Me.Controls() Where TypeOf (dd) Is
DropDownList Select dd
2# disbale the where value selected and enable others
For Each d In allDDs
d.Enabled = d.SelectedValue = ""
Next
say ddl1, ddl2, ddl3, ddl4, ddl5, ddl6 are ids of 6 dropdowns in your page.
for (int count = 1; count <= 6; count++)
{
DropDownList ddl = new DropDownList();
ddl.ID = "ddl" + count;
if (ddl.SelectedIndex > -1)
{
ddl.Enabled = false;
}
else
{
ddl.Enabled = true;
}
}
Related
How to remove list of datagridview at specific index c#
List selectedRows = (from row in dataGridView2.Rows.Cast()
where Convert.ToBoolean(row.Cells["checkBoxColumn"].Value) == true
select row).ToList();
when this linq statement execute it will add whole row in selectedRows, i do not want to add coulmn 0 and 1 in List selectedRows so how i will achieve this ?
you have to provide list of column you want to select in select new as given below that will resolve issue
List selectedRows = (from row in dataGridView2.Rows.Cast()
where Convert.ToBoolean(row.Cells["checkBoxColumn"].Value) == true
select new {
Field1 = row.Field<string>("Field1"),
Field2 = row.Field<string>("Field2")
}).ToList();
Yes its a window form
i make checkbox using collection property of datagridview named as Select
i want to execute below code upon changing status of checkbox to check
foreach (DataGridViewRow item in dataGridView1.Rows)
{
if ((bool)item.Cells[0].Value == true)
{
int n = dataGridView2.Rows.Add();
dataGridView2.Rows[n].Cells[0].Value = false;
dataGridView2.Rows[n].Cells[1].Value = item.Cells[1].Value.ToString();
dataGridView2.Rows[n].Cells[2].Value = item.Cells[2].Value.ToString();
dataGridView2.Rows[n].Cells[3].Value = item.Cells[3].Value.ToString();
dataGridView2.Rows[n].Cells[4].Value = item.Cells[4].Value.ToString();
dataGridView2.Rows[n].Cells[5].Value = item.Cells[5].Value.ToString();
dataGridView2.Rows[n].Cells[6].Value = item.Cells[6].Value.ToString();
dataGridView2.Rows[n].Cells[7].Value = item.Cells[7].Value.ToString();
dataGridView2.Rows[n].Cells[8].Value = item.Cells[8].Value.ToString();
dataGridView2.Rows[n].Cells[9].Value = item.Cells[9].Value.ToString();
dataGridView2.Rows[n].Cells[10].Value = item.Cells[10].Value;
for (int i = 0; i < dataGridView2.Columns.Count; i++)
if (dataGridView2.Columns[i] is DataGridViewImageColumn)
{
((DataGridViewImageColumn)dataGridView2.Columns[i]).ImageLayout = DataGridViewImageCellLayout.Stretch;
break;
}
foreach (DataGridViewRow row in this.dataGridView2.Rows)
{
row.Cells[0].Value = true;
}
}
}
above code is lies between button click event
I am trying to create a list of DDL using code behind as you can see here :
public List<DropDownList> ddll = new List<DropDownList>();
for (int i = 0; i < 15; i++)
{
DropDownList ansList = new DropDownList();
ansList.AutoPostBack = false;
ansList.DataSource = values1;
ansList.DataBind();
ddll.Add(ansList);
}
As you can see i set the autopostback attribute on false .But it doesn't work any my pages are refreshed when the user changes the selectedindex.
I added DDL using this :
Span1.Controls.Add(ddll[0]);
Span2.Controls.Add(ddll[1]);
Span3.Controls.Add(ddll[2]);
Span4.Controls.Add(ddll[3]);
Span5.Controls.Add(ddll[4]);
Span6.Controls.Add(ddll[5]);
Span7.Controls.Add(ddll[6]);
Span8.Controls.Add(ddll[7]);
Span9.Controls.Add(ddll[8]);
Span10.Controls.Add(ddll[9]);
Span11.Controls.Add(ddll[10]);
Span12.Controls.Add(ddll[11]);
Span13.Controls.Add(ddll[12]);
Span14.Controls.Add(ddll[13]);
Span15.Controls.Add(ddll[14]);
In html code i have this :
<span style="color:#ea0000;padding:0 10px;" id="Span6" runat="server"></span>
You first set AutoPostBack to false and later to true right after two statements. The false is overwritten by true and it should do postback now when the selected index is changed.
ansList.AutoPostBack = false;
//...
ansList.AutoPostBack = true;
Edit You can also use loop to add the list in the spans using FindControl(string id) to get the spans by id.
for(int i=0; i < 15; i++)
this.FindControl("Span"+i).Add(ddll[i]);
Look at your code
for (int i = 0; i < 15; i++)
{
DropDownList ansList = new DropDownList();
ansList.AutoPostBack = false; // Here You have set it false
ansList.DataSource = values1;
ansList.DataBind();
ansList.AutoPostBack = true; // Here You have set it true again
ddll.Add(ansList);
}
i have 4 dropdown list in my form, but when i want to set the selected value the first 2 ddl, always follow value the next 2 ddl.
aspx.cs
if(!Page.IsPostBack)
{
for (int hours = 0; hours <= 23; hours++)
{
ListItem li = new ListItem();
li.Text = hours.ToString();
li.Value = hours.ToString();
ddlDepartHours.Items.Add(li);
ddlArrivalHours.Items.Add(li);
}
for (int mins = 0; mins <= 55; mins+=5 )
{
ListItem li = new ListItem();
li.Text = mins.ToString();
li.Value = mins.ToString();
ddlDepartMins.Items.Add(li);
ddlArrivalMins.Items.Add(li);
}
ddlDepartHours.SelectedValue = "1";
ddlDepartMins.SelectedValue = "5";
ddlArrivalHours.SelectedValue = "10";
ddlArrivalMins.SelectedValue = "50";
}
this is the link of the result : http://imageshack.us/photo/my-images/69/ibte.png/
Note that you are inserting the same objects as list items for both "hours" as well as both "minutes" DDLs. Later on when you are setting the selected value of the one of them, effectively you are setting corresponding ListItem object as selected, which of course affects one DDL in which this object is also added.
Solution here is to insert different objects in different, though similar, dropdowns:
ddlDepartHours.Items.Add(new ListItem(hours.ToString(), hours.ToString()));
ddlArrivalHours.Items.Add(new ListItem(hours.ToString(), hours.ToString()));
The same for the minutes:
ddlDepartMins.Items.Add(new ListItem(mins.ToString(), mins.ToString()));
ddlArrivalMins.Items.Add(new ListItem(mins.ToString(), mins.ToString()));
Try to change the name of the object in the second ListItem li = new ListItem();
Add items like this
ddlDepartHours.Items.Add(new ListItem(hours.ToString(), hours.ToString()));
ddlArrivalHours.Items.Add(new ListItem(hours.ToString(), hours.ToString()));
ddlDepartMins.Items.Add(new ListItem(mins.ToString(), mins.ToString()));
ddlArrivalMins.Items.Add(new ListItem(mins.ToString(), mins.ToString()))
I have for example this values in datagridView : 1;2;3;4;
I want display this values in combobox like this : 1
2
3
4
My code show only last value in combobox: 4
My code of showing :
string cmbValue = CmbText;
string[] cmb = cmbValue.Split(new[] { ';' },StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < cmb.Length; i++)
{
comboBox1.Text = cmb[i];
}
and here is my code of creating array of combobox and calling method to change cell in dataGrid:
string cmbText = comboBox1.Text;
string[] cmb = new string[] { cmbText};
frm1.ChangeCellCmb(2, cmb);
this.Dispose();
Someone know how to do this ? I cant simply set collection of values combobox, because values of combobox are reading from datagrid and it's reading from DB.
Many thanks.
You need to loop and add the required items as follows:
for (int i = 0; i < cmb.Length; i++)
comboBox1.Items.Add(cmb[i]);
This will add all the required items to the drop down menu. To select/display '4' the 3rd entry in cmb by default do
comboBox1.SelectedIndex = 3;
or
comboBox1.SelectedItem = "4";
I hope this helps.
i have datagridview which has one combobox column. i populate combobox column. when i select any text from combobox column then i need to get the value when i read the data in for loop.
dgFilter is here datagridview
DataGridViewComboBoxColumn dgcoSpec = new DataGridViewComboBoxColumn();
dgcoSpec = new DataGridViewComboBoxColumn();
dgcoSpec.DataSource = loadOperators();
dgcoSpec.DisplayMember = "Operatortxt";
dgcoSpec.ValueMember = "Operatorvalue";
dgcoSpec.AutoComplete = true;
dgcoSpec.HeaderText = "Operators";
dgcoSpec.Name = "Operators";
dgcoSpec.DefaultCellStyle.NullValue = "--Select--";
dgcoSpec.Width = 130;
dgFilter.Columns.Insert(1, dgcoSpec);
here this way i read data from combobox column
for (int i = 0; i <= dgFilter.Rows.Count - 1; i++)
{
strOperator = dgFilter.Rows[i].Cells[1].Value.ToString().Trim();
}
but the problem is i am not getting the combox value member rather i am getting display member.
so how to extract value member from for loop. please guide me with code. thanks
The value member is the string that is displayed inside the DataGridViewComboboxCell.
The actual Combobox control only exists during the time frame in which the user is editing the cell.
If you mean that you would like to get the index of the value in the list of the DataGridViewComboboxCell items, you can query the index of the value:
for (int i = 0; i <= dgFilter.Rows.Count - 1; i++)
{
var cell = dgFilter.Rows[i].Cells[1] as DataGridViewComboboxCell;
int index = cell == null || cell.Value == null ? -1 : cell.Items.IndexOf(cell.Value);
Console.WriteLine(index);
}
In this example, I am using -1 as an invalid value.
EDIT Just noticed you are using a DataSource;
See DataGridViewComboBoxColumn name/value how? for possible duplicate.