bind many dropdownlist in a continous fashion - c#

Hi all i need to bind a list of dropdown to values in database.Each drop down will be inside a panel and i have named it as ddlxx1,ddlxx2,ddlxx3 in a continuous fashion. all these drop down list will have the same data source.Is there any way to bind these control in a loop or should i find control each time which is in panel then bind it?
Something like:
for(int i=1;i<=10;i++)
{
ddlxx+"i".DataSource = Prod.GetValues();
ddlxx+"i".DataTextField = "ComponentID";
ddlxx+"i".DataValueField = "ComponentName";
ddlxx+"i".DataBind();
}
Please help

If your drop down lists are added to the mark-up you can simply do the following:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
for(int i=1;i<=10;i++)
{
var ddl = FindControl("ddlxx" + i) as DropDownList;
if (ddl != null)
{
BindDropDown(ddl);
}
}
}
}
private void BindDropDown(DropDownDataList ddl)
{
ddl.DataSource = Prod.GetValues();
ddl.DataTextField = "ComponentID";
ddl.DataValueField = "ComponentName";
ddl.DataBind();
}

Try this:
for (int i = 1; i <= 10; i++)
{
DropDownList drp = (DropDownList)panel1.FindControl("ddlxx" + i.ToString());//panel1 is your panel which contain dropdown ddlxx1,ddlxx2,ddlxx3...
drp.DataSource = Prod.GetComponents();
drp.DataTextField = "ComponentID";
drp.DataValueField = "ComponentName";
drp.DataBind();
}

Related

Overlapping in listview c#

Good day, I have a list view that looks like this.
My problem is that the name of the items are overlapping, that name is from the database, do i have to set something in the properties for it not to overlap?
form_load code
private void FormAdhocReport_Load(object sender, EventArgs e)
{
LoadReport();
ToolBar(sRights);
}
LoadReport code:
private void LoadReport()
{
dsReports.Clear();
this.listReports.Items.Clear();
adhoc.AccountRowID = CurrentUserNameRowID;
string sRetXMLValue = adhoc.get_sp_Reports_View_Owner();
string XMLDOC = sRetXMLValue;
ReadXMLData(XMLDOC);
ListReportData();
//int count = listReports.Items.Count;
}
ListReportDataCode:
private void ListReportData()
{
try
{
// Get the table from the data set
DataTable dtable = dsReports.Tables[0];
// Clear the ListView control
this.listReports.Items.Clear();
// Display items in the ListView control
for (int i = 0; i < dtable.Rows.Count; i++)
{
DataRow drow = dtable.Rows[i];
if (drow.RowState != DataRowState.Deleted)
{
// Define the list items
ListViewItem lvi = new ListViewItem(drow["ReportName"].ToString());
lvi.ImageIndex = 0;
lvi.SubItems.Add(drow["RowID"].ToString());
listReports.Items.Add(lvi);
}
}
}
catch { }
}
Don't mind this codes: string sRetXMLValue = adhoc.get_sp_Reports_View_Owner();
string XMLDOC = sRetXMLValue;
ReadXMLData(XMLDOC); because they're just for getting the data in the database.
Thanks in advance for your help.

DropdownList SelectedValue property doesn't accept value during edit operation

I have a cascading dropdownlist inside a gridview which contains 5 columns which are ItemsType, ItemList, UnitPrice, Quantity and Total. Selecting ItemsType in 1st dropdownlist will populate items in 2nd dropdownlist i.e. ItemList which is bound to database.
Problem that I am getting is that when I click Edit button, items in 2nd dropdownlist Itemstype isn't selected though I have assigned the selectedValue.
When I ran the webform in debugging mode I saw that though value has been rightly assigned to dropdownlist.selectedvalue, it isn't accepting the value at all.
The weird thing is that when I click the edit button for the second time after 1st debug, items in dropdownlist are selected correctly.
I have used the following
ddlCarriedItems.SelectedValue = SValue;
Though SValue has values of itemlist, SelectedValue displays none.
Here's the code.
protected void btnEdit_Click(object sender, EventArgs e)
{
try
{
Button btn = (Button)sender;
GridViewRow gv = (GridViewRow)btn.NamingContainer;
string VRM_id = gv.Cells[0].Text;
dc.Company_code = Convert.ToInt32(Session["company_code"].ToString());
dc.Vrm_id = Convert.ToInt32(VRM_id);
DataTable dtVRM_CP = vrmbll.edit_VRM_carried_products(dc);
int rowIndex = 0;
if (dtVRM_CP.Rows.Count > 0)
{
for (int i = 0; i < dtVRM_CP.Rows.Count; i++)
{
DropDownList ddlItemsType = (DropDownList)gvCarriedItems.Rows[rowIndex].Cells[1].FindControl("ddlItemsType");
DropDownList ddlCarriedItems = (DropDownList)gvCarriedItems.Rows[rowIndex].Cells[2].FindControl("ddlCarriedItems");
TextBox txtItemPrice = (TextBox)gvCarriedItems.Rows[rowIndex].Cells[3].FindControl("txtItemPrice");
TextBox txtItemQty = (TextBox)gvCarriedItems.Rows[rowIndex].Cells[4].FindControl("txtItemQty");
TextBox txtItemTotal = (TextBox)gvCarriedItems.Rows[rowIndex].Cells[5].FindControl("txtItemTotal");
ddlItemsType.SelectedIndex = Convert.ToInt32((dtVRM_CP.Rows[i]["items_type"]).ToString());
int itemstype = ddlItemsType.SelectedIndex;
string SValue = dtVRM_CP.Rows[i]["items"].ToString();
if (itemstype == 1)
{
ddlCarriedItems.SelectedValue =SValue;
}
else if (itemstype == 2)
{
ddlCarriedItems.SelectedValue = SValue;
}
else if (itemstype == 3)
{
ddlCarriedItems.SelectedValue = SValue;
}
else
{ }
txtItemPrice.Text = dtVRM_CP.Rows[i]["items_price"].ToString();
txtItemQty.Text = dtVRM_CP.Rows[i]["items_qty"].ToString();
txtItemTotal.Text = dtVRM_CP.Rows[i]["items_total"].ToString();
if (i != (dtVRM_CP.Rows.Count) - 1 && gvCarriedItems.Rows.Count != (dtVRM_CP.Rows.Count))
{
AddNewRow();
}
rowIndex++;
}
}
}
catch
{ }
}
Ok I've found the problem in my code. The thing is that I had to bind dropdownlist everytime before assigning value to ddlCarriedItems.SelectedValue.
I modified my code as following as per #king.code suggestion:
if (itemstype == 1)
{
DataTable dt = vrmbll.select_raw_materials_production(dc);
ddlCarriedItems.DataSource = dt;
ddlCarriedItems.DataValueField = "ID";
ddlCarriedItems.DataTextField = "Name";
ddlCarriedItems.DataBind();
ddlCarriedItems.Items.Insert(0, new ListItem("----- Select -----", "-1"));
ddlCarriedItems.SelectedValue = SValue;
}
But I have no idea why the previous code worked when I ran debugging for 2nd time.

create a dropdonlist and add item on onload function

I have created a dropdown list. I have added item to dropdown list on onload function.
protected void Page_Load(object sender, EventArgs e)
{
int x = DateTime.Now.Year;
List<string> str = new List<string>();
for (int i = x; i >= 1975; i--)
{
str.Add(i.ToString());
}
ddlYear.DataSource = str;
ddlYear.DataBind();
}
But when I click on button to submit the selected value of dropdownlist to database it always goes 2013 in my database. ddlYear is my dropdownlist id.
When you click the submit button page makes postback. Because of this reason dropdownlist selected value 2013. Change your code like this.
if (!Page.IsPostBack)
{
int x = DateTime.Now.Year;
List<string> str = new List<string>();
for (int i = x; i >= 1975; i--)
{
str.Add(i.ToString());
}
ddlYear.DataSource = str;
ddlYear.DataBind();
}
it is not clear what you are submitting to the database:
Try submitting ddlyear.SelectedItem.Value to the database.

Tag Array c# winforms

The code below lets me show emails received in a listview on when the selected index is changed displays the body of the selected email in a RTB. The problem is i changed the code to work with a data grid view and now the Tag part wont work
void SomeFunc() // This line added by Jon
{
int i;
for (i = 0; i < bundle.MessageCount; i++)
{
email = bundle.GetEmail(i);
ListViewItem itmp = new ListViewItem(email.From);
ListViewItem.ListViewSubItem itms1 =
new ListViewItem.ListViewSubItem(itmp, email.Subject);
ListViewItem.ListViewSubItem itms2 =
new ListViewItem.ListViewSubItem(itmp, email.FromName);
itmp.SubItems.Add(itms1);
itmp.SubItems.Add(itms2);
listView1.Items.Add(itmp).Tag = i;
richTextBox1.Text = email.Body;
}
// Save the email to an XML file
bundle.SaveXml("email.xml");
}
private void listView1_SelectionChanged(object sender, EventArgs e)
{
if (listView1.SelectedCells.Count > 0)
{
// bundle is now accessible in your event handler:
richTextBox1.Text = bundle.GetEmail((int)listView1.SelectedCells[0].Tag).Body;
}
}
Code for data grid view
int i;
for (i = 0; i < bundle.MessageCount; i++)
{
email = bundle.GetEmail(i);
string[] row = new string[] { email.From, email.Subject, email.FromName };
object[] rows = new object[] { row };
foreach (string[] rowArray in rows)
{
dataGridView1.Rows.Add(rowArray);
}
} // This line added by Jon
i have created earlier the code for datagrid view but you already done it so i haven't posted in your last question but i think , you should give a try to the below code.
// i am creating a new object here but , you can have a single object on the form
DataGridView dgv = new DataGridView();
private DataTable EmailSource { get; set; }
dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dgv.SelectionChanged+=new EventHandler(dgv_SelectionChanged);
Chilkat.MessageSet msgSet = imap.Search("ALL", true);
if (msgSet != null)
{
bundle = imap.FetchBundle(msgSet);
CreateDataTable();
if (bundle != null && dt!=null)
{
Chilkat.Email email;
int i;
for (i = 0; i < bundle.MessageCount; i++)
{
email = bundle.GetEmail(i);
if(email!=null)
{
DataRow drow = EmailSource.NewRow();
drow["Id"] = i.ToString();
drow["From"] = email.FromName;
drow["Subject"] = email.Subject;
drow["DateRecived"] = email.DateRecived;
// i am adding email body also
drow["Body"] =email.Body;
EmailSource.Rows.Add(drow);
}
}
// Save the email to an XML file
bundle.SaveXml("email.xml");
dgv.DataSource= EmailSource;
// Hiding Body from the grid
dgv.Columns["Body"].Visible =false;
}
}
// this event handler will show the last selected email.
void dgv_SelectionChanged(object sender, EventArgs e)
{
DataGridViewSelectedRowCollection rows = dgv.SelectedRows;
if (rows != null)
{
// get the last selected row
DataRow drow = rows[rows.Count - 1].DataBoundItem as DataRow;
if (drow != null)
{
richTextBox1.Text = drow["Body"];
}
}
}
private void CreateDataTable()
{
EmailSource = new DataTable();
EmailSource.Columns.Add("Id");
EmailSource.Columns.Add("From");
EmailSource.Columns.Add("Subject");
EmailSource.Columns.Add("DateRecived");
EmailSource.Columns.Add("Body");
}
You are adding rows using listView1.Rows.Add(rowArray) in both the code listings. Is that a typo or you named the GridView like that.
Basically, you are storing the index of the email in the "Tag" property.
listView1.Items.Add(itmp).Tag = i;
You need to make sure that you do the same while adding items to the GridView too.
The DataGridView does not have an "Items" collection. To make it work, you need to bind the DataGridView to a collection of objects. Something like this should get you started:
List<Email> emails = new List<Email>();
for (i = 0; i < bundle.MessageCount; i++)
{
email = bundle.GetEmail(i);
emails.Add(email);
}
dataGridView.ItemsSource = emails;
You should not need to store the row index for each item in a "Tag" object - you can can get the selected index like this:
int selectedIndex = dataGridView.SelectedCells[0].RowIndex;

c# combobox has a datasource, how do i make it display a specific element?

i have a datatable as the source of a combobox:
1
3
2
4
5
without knowing the order of the elemnts inside the datatable, and only knowing the exact text, is it possible to display a specific element like '4' ?
so you should do this:
comboBox1.Text = "4";
or change the value of related field in DataTable:
((MyDataRowType)((DataRowView)bindingSource1.Current).Row).myFieldName = "4";
In DataBound event of your dropdownlist try this:
protected void DropDownList1_DataBound(object sender, EventArgs e)
{
for (int i = 0; i < DropDownList1.Items.Count; i++)
{
if (DropDownList1.Items[i].Text == "4")
{
DropDownList1.SelectedIndex = i;
}
}
}
myComboBox.SelectedItem = "4";

Categories