I am trying to list all combobox items in one messagebox. but all i get is every item comes up in its own messagebox. I know the messagebox needs to be outside the loop but when i do that it says the variable is unassigned. Any help would be great.
private void displayYachtTypesToolStripMenuItem_Click(object sender, EventArgs e)
{
string yachtTypesString;
for (int indexInteger = 0; indexInteger < typeComboBox.Items.Count; indexInteger++)
{
yachtTypesString=typeComboBox.Items[indexInteger].ToString();
MessageBox.Show(yachtTypesString);
}
}
Do it like this,
StringBuilder yachtTypesString = new StringBuilder();
for (int indexInteger = 0; indexInteger < typeComboBox.Items.Count; indexInteger++)
{
yachtTypesString.AppendLine(typeComboBox.Items[indexInteger].ToString());
}
MessageBox.Show(yachtTypesString.ToString());
NOTE: Do not do string concatenation with a string, use StringBuilder object as doing it in a string creates a new instance.
You can try using Linq:
MessageBox.Show(String.Join(Environment.NewLine, typeComboBox.Items.Cast<String>()));
and let it do all the work for you
Try this
string yachtTypesString="";
for (int indexInteger = 0; indexInteger < typeComboBox.Items.Count; indexInteger++)
{
yachtTypesString=yachtTypesString + typeComboBox.Items[indexInteger].ToString();
}
MessageBox.Show(yachtTypesString);
list all combobox items in one messagebox
Please play with Below code To Get Combobox all Text
private void Form1_Load(object sender, EventArgs e)
{
DataTable dtcheck = new DataTable();
dtcheck.Columns.Add("ID");
dtcheck.Columns.Add("Name");
for (int i = 0; i <= 15; i++)
{
dtcheck.Rows.Add(i, "A" + i);
}
comboBox1.ValueMember = "ID";
comboBox1.DisplayMember = "Name";
comboBox1.DataSource = dtcheck;
}
}
private void button1_Click(object sender, EventArgs e)
{
string MessageText = string.Empty;
foreach(object item in comboBox1.Items)
{
DataRowView row = item as DataRowView;
MessageText += row["Name"].ToString() + "\n";
}
MessageBox.Show(MessageText, "ListItems", MessageBoxButtons.OK,MessageBoxIcon.Information);
}
Related
I am trying to output my selected rows in the AspxGridView to a label to see what has been selected. However the result is "System.Collections.Generic.List`1[System.Object]" Rather than the text in column called "ID"
protected void Button13_Click(object sender, EventArgs e)
{
List<string> itemList = new List<string>();
Label2.Text = string.Empty;
for (int i = 0; i < ASPxGridView1.VisibleRowCount; i++)
{
if (ASPxGridView1.Selection.IsRowSelected(i))
{
itemList.Add(ASPxGridView1.GetSelectedFieldValues("ID").ToString());
}
}
Label2.Text = string.Join("<br />", itemList);
}
If you want to select one row only and get its values on a click, I advise you to use the FocusedRowChanged event of the ASPxGridView.
To use it, you first need to enable FocusedRow.
Then if you want "ID" only, you can select it directly
Label2.Text = string.Join("<br />", ASPxGridView1.GetSelectedFieldValues("ID").ToString());
You can use :
foreach (var dt in ASPxGridView1.GetSelectedFieldValues("ID").ToList())
{
Label2.Text = Label2.Text + dt;
}
I have filled listBox with data and assigning it to gridview but it doesn't. I verified by setting count of list to variable and it shows 3, perfect but after assigning it to gridview, the count of gridview shows 0. Why ?
protected void btnShowTempFeatures_Click(object sender, EventArgs e)
{
try
{
int count = ListBoxFeatures.Items.Count; //returns 3
grdViewTemporaryFeatures.DataSource = ListBoxFeatures.DataSource;
grdViewTemporaryFeatures.DataBind();
int CountGrid= grdViewTemporaryFeatures.Rows.Count; //return 0
}
}
Solved
protected void btnShowTempFeatures_Click(object sender, EventArgs e)
{
try
{
int count = ListBoxFeatures.Items.Count;
//grdViewTemporaryFeatures.DataSource = ListBoxFeatures.DataSource;
//grdViewTemporaryFeatures.DataBind();
int CountGrid= grdViewTemporaryFeatures.Rows.Count;
ListItemCollection lstTempFeatures = ListBoxFeatures.Items;
DataTable dTempFeatures = new DataTable();
dTempFeatures.Columns.Add("ID");
dTempFeatures.Columns.Add("FeatureName");
foreach (ListItem lstItem in lstTempFeatures)
{
DataRow dr = dTempFeatures.NewRow();
dr["ID"]= lstItem.Value;
dr["FeatureName"] = lstItem.Text;
dTempFeatures.Rows.Add(dr);
}
grdViewTemporaryFeatures.DataSource = dTempFeatures;
grdViewTemporaryFeatures.DataBind();
mdlTemporaryFeatures.Show();
}
I read text to the GridView and then move this text from the GridView to TextBoxes to edit. How can I replace the changed data from selection line and save to my text file?
this is my write file
private void button1_Click(object sender, EventArgs e)
{
using (System.IO.StreamWriter file =
new System.IO.StreamWriter(#"WriteLines2.txt", true))
{
file.WriteLine(this.id.Text + "#" + this.judul.Text + "#" + this.isi.Text);
}
}
this is my gridview
System.IO.StreamReader file = new System.IO.StreamReader("WriteLines2.txt");
string[] columnnames = file.ReadLine().Split('#');
DataTable dt = new DataTable();
foreach (string c in columnnames)
{
dt.Columns.Add(c);
}
string newline;
while ((newline = file.ReadLine()) != null)
{
DataRow dr = dt.NewRow();
string[] values = newline.Split('#');
for (int i = 0; i < values.Length; i++)
{
dr[i] = values[i];
}
dt.Rows.Add(dr);
}
file.Close();
GridView.DataSource = dt;
this is my grid view move to textbox
private void GridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
DataGridViewRow dr = GridView.SelectedRows[0];
id.Text = dr.Cells["id"].Value.ToString();
// or simply use column name instead of index
//dr.Cells["id"].Value.ToString();
judul.Text = dr.Cells["judul"].Value.ToString();
isi.Text = dr.Cells["subjek"].Value.ToString();
//textBox4.Text = dr.Cells[3].Value.ToString();
GridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
}
You should call the GridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect; when you initialize your DataGridView and not during the CellContentClick. Then use the SelectionChanged Event instead of the CellContentClick Event and use CurrentCell.RowIndex for the index:
private void GridView_SelectionChanged(object sender, EventArgs e)
{
DataGridViewRow dr = GridView.Rows[GridView.CurrentCell.RowIndex];
id.Text = dr.Cells[0].Value.ToString();
judul.Text = dr.Cells[1].Value.ToString();
isi.Text = dr.Cells[2].Value.ToString();
}
Your buton1_Click adds a new values to the end of your file. I'm not sure if that is what you want.
how can I change the character casing in my listview to uppercase? the items in listview should be in uppercase when I choose uppercase in combobox. I hope someone can help me with this. Thanks in advance.
private void Form1_Load(object sender, EventArgs e)
{
showlv("SELECT a.customer_name, a.address, b.product_name, b.price FROM tbl_customer AS a INNER JOIN tbl_transaction AS b WHERE a.customer_code = b.customer_code", lvcust);
}
private void showlv(string sql, ListView lv)
{
try
{
lvcust.View = View.Details;
lvcust.FullRowSelect = true;
lvcust.GridLines = true;
conn.Open();
MySqlDataAdapter sda = new MySqlDataAdapter(sql, conn);
DataTable dt = new DataTable();
sda.Fill(dt);
conn.Close();
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow dr = dt.Rows[i];
ListViewItem lvitem = new ListViewItem(dr["customer_name"].ToString());
lvitem.SubItems.Add(dr["address"].ToString());
lvitem.SubItems.Add(dr["product_name"].ToString());
lvitem.SubItems.Add(dr["price"].ToString());
lvcust.Items.Add(lvitem);
}
string[] column = new string[4] { "Customer Name", "Address", "Product Name", "Price" };
for (int x = 0; x < column.Length ; x++)
{
lvcust.Columns.Add(column[x]);
}
}
catch (Exception er)
{
MessageBox.Show(er.Message);
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedItem.Equals("Ascend"))
{
lvcust.Sorting = SortOrder.Ascending;
}
else if (comboBox1.SelectedItem.Equals("Descend"))
{
lvcust.Sorting = SortOrder.Descending;
}
else if (comboBox1.SelectedItem.Equals("Uppercase"))
{
//code to uppercase items in listview
}
}
You would be better off adding your case changing method in the event handler for the checkbox to upload it.
So, you doubleclick the checkbox control, then you iterate through the items in the combobox, then on each iteration set the content of the item to itself, with a .ToUpper() at the end.
I'm assuming you want to uppercase the customer name only. The trick is to store the original value as the tag of the ListItem. That way you can change the Text back to the original (non-uppercase) value later if you wanted. So in your code, find the first line and add the second below:
ListViewItem lvitem = new ListViewItem(dr["customer_name"].ToString());
lvitem.Tag = dr["customer_name"].ToString();
Now that you have that, here's the for loop to convert it to upper case:
ListViewItemCollection items = lvcust.Items;
for(int i=0;i<items.Count;i++){
ListViewItem item = items.Item[i];
object tag = item.Tag;
if(tag is string){
item.Text = ((string)tag).ToUpper();
}
}
This was all done off the top of my head in a text editor so there may be a syntax issue here or there but the logic should be correct.
I am attempting to fill a combobox with name and values from a appsettings file. Once a name is selected from the combobox I would like to have the value sent to a textbox below. I guess the part I am confused about is how to determine which is selected and display that value.
My goal would be to select Name "cmd" from combobox and have the value of path/to/cmd.exe into textbox below.
public void Form1_Load(object sender, EventArgs e)
{
string[] names = ConfigurationManager.AppSettings.AllKeys;
NameValueCollection appStgs = ConfigurationManager.AppSettings;
for (int i = 0; i < appStgs.Count; i++)
{
comboBox1.Items.Add(names[i]);
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string[] names = ConfigurationManager.AppSettings.AllKeys;
NameValueCollection appStgs = ConfigurationManager.AppSettings;
for (int i = 0; i < appStgs.Count; i++)
{
textBox3.Text = appStgs[comboBox1.Text];
}
}
Try something like this
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox3.Text = appStgs[comboBox1.Text];
}