Devexpress - Output selected Rows to label - c#

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;
}

Related

DataGridView In C# to select data and show it to one textbox

I am working on a windows form application, I have made datagridview for showing my data but I also want to select some specific columns from Datagridview and show them to only one TextBox.
I have tried but it is only getting last Column named "SEIKEN_NO" to textbox but I want multiple column values to show in One TextBox.
private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
// to set oem no, fic_no and seiken_no to textbox named particular1Txt.
dataGridView1.Refresh();
try
{
if (RB1.Checked == true)
{
int i;
i = dataGridView1.SelectedCells[0].RowIndex;
Particular1Txt.Text = dataGridView1.Rows[i].Cells["FIC_No"].Value.ToString();
Particular1Txt.Text = dataGridView1.Rows[i].Cells["OEM_No"].Value.ToString();
Particular1Txt.Text = dataGridView1.Rows[i].Cells["Seiken_NO"].Value.ToString();
}
else if (RB2.Checked == true)
{
int i;
i = dataGridView1.SelectedCells[0].RowIndex;
Particular2Txt.Text = dataGridView1.Rows[i].Cells["FIC_No"].Value.ToString();
Particular2Txt.Text = dataGridView1.Rows[i].Cells["OEM_No"].Value.ToString();
Particular2Txt.Text = dataGridView1.Rows[i].Cells["Seiken_NO"].Value.ToString();
}
Fore each line you assign data to the TextBox:
Particular1Txt.Text = dataGridView1.Rows[i].Cells["FIC_No"].Value.ToString();
change to:
Particular1Txt.Text += dataGridView1.Rows[i].Cells["FIC_No"].Value.ToString();
note the +=
otherwise you are overwriting the content of the TextBox
For values concatenation formatting, you can:
+= " " + dataGridView1.Rows[i].Cells["FIC_No"].Value.ToString(); // very basic way to do it
Another way example with String.Format()
Particular1Txt.Text=String.Format({0} {1} {2}), dataGridView1.Rows[i].Cells["FIC_No"].Value.ToString(),dataGridView1.Rows[i].Cells["OEM_No"].Value.ToString(),dataGridView1.Rows[i].Cells["Seiken_No"].Value.ToString();
You can also use: String.Join() with an string array, or the StringBuilder class,
hope this help you
private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
// to set oem no, fic_no and seiken_no to textbox named particular1Txt.
dataGridView1.Refresh();
try
{
if (RB1.Checked == true)
{
int i;
i = dataGridView1.SelectedCells[0].RowIndex;
Particular1Txt.Text = dataGridView1.Rows[i].Cells["FIC_No"].Value.ToString()+" "+ dataGridView1.Rows[i].Cells["OEM_No"].Value.ToString()+" "+dataGridView1.Rows[i].Cells["Seiken_NO"].Value.ToString();
}
else if (RB2.Checked == true)
{
int i;
i = dataGridView1.SelectedCells[0].RowIndex;
Particular1Txt.Text = dataGridView1.Rows[i].Cells["FIC_No"].Value.ToString()+" "+ dataGridView1.Rows[i].Cells["OEM_No"].Value.ToString()+" "+dataGridView1.Rows[i].Cells["Seiken_NO"].Value.ToString();
}
}
}

uppercase items in listview C#

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.

list all combobox items in one messagebox

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);
}

What to do to get rows respective to checked checkboxes and then write out the values?

Hello fellow programmers,
Right now I have a webpage which displays a data-table via grid-view. This works fine. I also inserted a column of check-boxes in the beginning of the view. This works fine as well. But when I try to retrieve info from cells from the row whose respective check-box is selected, I get an empty string.
Below are the parts of my code pertinent to this problem:
.aspx:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
return;
}
else
{
}
//server connections and whatnot//
OleDbCommand c0 = new OleDbCommand(sql0, myConnection);
myAdapter.SelectCommand = c0;
DataSet ds0 = new DataSet("Contacts");
myAdapter.Fill(ds0);
DataTable dt0 = new DataTable();
dt0 = ds0.Tables["Contacts"];
DataView view = new DataView();
view.Table = dt0;
GV0.DataSource = view;
GV0.DataBind();
myConnection.Close();
}
.cs:
/**
* WHY U NO WORK?!
*/
public void Dedupe(Object o, EventArgs e)
{
String output = "start ";
foreach (GridViewRow row in GV0.Rows)
{
CheckBox cb = (CheckBox)row.FindControl("CheckBox1");
if (cb.Checked == true)
{
output += row.Cells[1];
}
}
Label1.Text = output;
}
Any help would be greatly appreciated.
Cheers
Just a thought but in your foreach loop you may need to check the RowType property of the current row that you are looping over and make sure that it is set to DataRow. Or you could use a LINQ statement to bring back only rows with a RowType == DataRow and loop over that.
UPDATE Here is a quick sample of what I meant. But in reviewing your code it does not appear that you have any text in the second cell of your gridview, in fact your gridview only has one cell based on the code you provided.
public void Dedupe(Object o, EventArgs e)
{
String output = "start ";
IEnumerable<GridViewRow> rows = from r in GV0.Rows
where r.RowType == DataControlRowType.DataRow
select r;
foreach (GridViewRow row in rows)
{
CheckBox cb = (CheckBox)row.FindControl("CheckBox1");
if (cb.Checked)
{
output += row.Cells[1];
}
}
Label1.Text = output;
}

getting the rowindex of gridview based on the label control value

I have a label control on my page and beneath the label control there is a Gridview.The value that exists in the label control also exists in the Gridview. The label control has value like this
3244|Yellow Ink| Test Link
In the gridview, I have a value 3244 too
3244 yello Ink Test Link
3255 Green Link Test2
I want the row Index of 3244 in my code behind as soon as the page loads. Is their any way to do it.
Not knowing what type of data source you have, one way of doing it would be with LINQ like this:
protected void Page_Load(object sender, EventArgs e)
{
// Fetch the text from your label. (I'm assuming that you have only one label with the text "3244|Yellow Ink| Test Link".
string text = Label1.Text;
// Find the first row or return null if not found.
var resultRow = GridView1.Rows.Cast<GridViewRow>().FirstOrDefault(row =>
{
// Get the id (that I'm guessing is the first (0-index) column/cell)
var id = row.Cells[0].Text;
// Return true/false if the label text starts with the same id.
return text.StartsWith(id);
});
if (resultRow != null)
{
var index = resultRow.RowIndex;
}
}
A shorter version would look like this:
var resultRow = GridView1.Rows.Cast<GridViewRow>()
.FirstOrDefault(r => text.StartsWith(r.Cells[0].Text));
protected void btnJobAppSelected_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridViewJobApplications.Rows)
{
int rowIndex = ((sender as LinkButton).NamingContainer as GridViewRow).RowIndex;
LinkButton btnJobAppSelected = (LinkButton)GridViewJobApplications.Rows[rowIndex].FindControl("btnJobAppSelected");
}
}
In your page load after populating gridView, you can do that like this:
String searchValue = "3244 ";
int rowIndex = -1;
foreach (GridViewRow row in GridViewID.Rows)
{
if (row.Cells[0].Text.ToString().Equals(searchValue))
{
rowIndex = row.RowIndex;
break;
}
}
int YourIndex = rowIndex;

Categories

Resources