My gridview is populated with Article object like this
var sel = (Article)cmbArticleList.SelectedItem;
DataRow newRow = articlesTable.NewRow();
newRow["CODE"] = sel.Code;
newRow["NAME"] = sel.Name;
newRow["PRICE"] = sel.Price;
articlesTable.Rows.Add(newRow);
articlesGridView.DataSource = articlesTable;
I'm wonder how can I recognize selected row of this grid, for example on LabelSelectedRow.Text should be populated with selected row Code text.
First off you can get your selected rows like so;
//For multiple row selection.
IList rows = dg.SelectedItems;
//For single row selection;
DataRowView row = (DataRowView)dg.SelectedItems[0];
//You can then access them via their column name;
row["yourColumnName"];
//So to assign to say your label..
LabelSelectedRow.Text = row["CODE"] + " " + " row[NAME] + " " + row[PRICE];
Edit: You can put this code in one of the datagrid click events, perhaps RowSelected.
Related
I have an home work i almost finished it all but i could not get this one
this is my data grid view columns
Name , Price , Quantity , Total , button(to increase) , button (to decrease)
in the data Grid View i made a button when i double click on the increase button the quantity will increase by 1 every time
so i made this code to that
foreach (DataGridViewRow row in dataGridView1.Rows)
{
row.Cells["Qty"].Value = Convert.ToDouble(row.Cells["Add"].Value) + Convert.ToDouble(row.Cells["Qty"].Value) + 1;
}
but it do the Entire column if i have 2 rows when i double click on it the quantity of both rows is increasing
how can i change that code to just 1 row for the button
You are looping over all rows with for statement.
You should use the CurrentCell.RowIndex property to get the current selected row.
So you code should look like:
var row = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex];
row.Cells["Qty"].Value = Convert.ToDouble(row.Cells["Add"].Value) + Convert.ToDouble(row.Cells["Qty"].Value) + 1;
the problem is the foreach.
in the button event you need to get the row index.
You can get the row index with
var index = datagridview.CurrentCell.RowIndex;
Than you can do something like this:
dataGridView1.Rows[index].Cells["Qty"].Value =
Convert.ToDouble(dataGridView1.Rows[index].Cells["Add"].Value)
+ Convert.ToDouble(dataGridView1.Rows[index].Cells["Qty"].Value) + 1;
need help on getting the values from datagridview to combobox, I am having a datagridview getting data's from SQL which counts 5 columns and number of rows retrieved from SQL, I am having 4 textboxes & 2 comboboxes.
I have written code for textbox & combobox like below.
int i;
i = dataGridView1.SelectedCells[0].RowIndex;
textBox4.Text = dataGridView1.Rows[i].Cells[0].Value.ToString();
textBox3.Text= dataGridView1.Rows[i].Cells[1].Value.ToString();
comboBox1.SelectedValue = dataGridView1.Rows[i].Cells[2].Value.ToString();
textBox5.Text = dataGridView1.Rows[i].Cells[3].Value.ToString();
textBox1.Text = dataGridView1.Rows[i].Cells[4].Value.ToString();
comboBox2.SelectedIndex = Convert.ToInt32(dataGridView1.Rows[i].Cells[4].Value.ToString());
I'm getting textbox values perfectly but want the datagridview selected row's column values to the combobox.
Do not use SelectedIndex, use SelectedValue.
Try this:
comboBox1.DisplayMember = "MyFieldText";
comboBox1.ValueMember = "MyFieldId";
var myObject = (MyObject)dataGridView1.SelectedRows[0].DataBoundItem;
comboBox1.SelectedValue = myObject.Id;
If you want to add these strings to comboBox you should not use SelectedIndex property.
If I understand your intention correctly here is what I think you are looking for:
comboBox2.Items.Add(dataGridView1.Rows[i].Cells[4].Value.ToString());
I would like to search my DataSet for a customer name, this name will occur more than once throughout the dataset, meaning there will be more than 1 result (multiple rows returned)
When my search button is clicked, form2 opens up, and in theory should display all rows containing the customer name, but how do I achive this?
I can get it to display 1 row without a problem, but how would I make it display all rows, and the data in textboxes.
At the moment I am using a foreach loop to fill text boxes with returned data, but this only takes data from 1 row within my DataSet.
Is there a way I can make my form auto generate textboxes and populate them will all the data from the array? When I run my query at the moment, it fills the textboxes on form2 with the last rotation of the foreach.
DB Sample
Appreciate any help
Adam
DataRow[] returnedRows;
returnedRows = ds.Tables["Table_Data_1"].Select("cust_name='" + searchFor + "'");
foreach (DataRow returned in returnedRows)
{
tbName.Text = returned[1].ToString();
tbAddress.Text = returned[2].ToString();
tbPhone.Text = returned[3].ToString();
tbMake.Text = returned[4].ToString();
tbModel.Text = returned[5].ToString();
tbReg.Text = returned[6].ToString();
tbYear.Text = returned[7].ToString();
tbParts1.Text = returned[8].ToString();
tbParts2.Text = returned[9].ToString();
tbParts3.Text = returned[10].ToString();
tbParts3.Text = returned[11].ToString();
}
The reason you're having just a single value appear is that you're setting the text of your text boxes to a new value for each row that you've selected. You could change your code to instead add to the Text in the the Textboxes:
tbName.Text += returned[1].ToString() + Environment.NewLine;
Or you could instead bind it to a DataGridView, which is designed to display tabular data. Assuming the DataGridView was named something like customerDataView,
returnedRows = ds.Tables["Table_Data_1"].Select("cust_name='" + searchFor + "'");
var newTable = returnedRows.CopyToDataTable();
BindingSource bindSource = new BindingSource();
bindSource.DataSource = newTable;
var customerDataView.DataSource = bindSource;
You can generate textboxes dinamicaly
Its something like this
foreach (DataRow returned in returnedRows)
{
TextBox txt = new TextBox();
txt.ID = "textBox1"; //generate id dinamically can be a count
txt.Text = returned[1].ToString();
form1.Controls.Add(txt);
}
The way you've implemented the loop override the data in each interation
I've improved the sample code from following this post
https://stackoverflow.com/a/2229040/5252904 answer provided by #rahul.
I have a datagridview, I am binding this grid with SQL, I have added five rows in datagrid as same in sql with autogeneratecolumn = false..
my code is
string qry1 = "select modal,Spec,color,Types,reqQty from godownRequsition where gr='" + txtGr.Text + "'";
dataGridView1.DataSource = DAL.GetdataTable(qry1);
the problem is that when I run the program, the rows of datagrid could not show the data. If I finish all the column in datagrid and autogenerate column is true it show the data..
now I want to bind the datagrid with my own generated columns not autogenerated.
You need to create the column by your self like this :
DataGridViewColumn column = new DataGridViewTextBoxColumn();
column.HeaderText = "Lama";
int indexCol = grid.Columns.Add(column);
I'd like to bind a ComboBox to a DataTable (I cannot alter its original schema)
cbo.DataSource = tbldata;
cbo.DataTextField = "Name";
cbo.DataValueField = "GUID";
cbo.DataBind();
I want the ComboBox show tbldata.Name + tbldata.Surname.
Of course adding the new name+surname as a field to the tbldata just before binding is possible, but I am hoping for a more elegant solution along the lines of (pseudocode)
cbo.DataTextField = "Name";
cbo.DataTextField += "Surname";
The calculated column solution is probably the best one. But if you can't alter the data table's schema to add that, you can loop through the table and populate a new collection that will serve as the data source.
var dict = new Dictionary<Guid, string>();
foreach (DataRow row in dt.Rows)
{
dict.Add(row["GUID"], row["Name"] + " " + row["Surname"]);
}
cbo.DataSource = dict;
cbo.DataTextField = "Value";
cbo.DataValueField = "Key";
cbo.DataBind();
Obviously this isn't as performant as binding directly to the DataTable but I wouldn't worry about that unless the table has thousands of rows.
The easiest way is to create a new calculated column in the DataTable, using the Expression property :
tbldata.Columns.Add("FullName", typeof(string), "Name + ' ' + Surname");
...
cbo.DataTextField = "FullName";
I would create a property on your data object then map that to the DataTextField
Data Object
public string FullName
{
get { return Name + " " + Surname; }
}
Code-behind
cbo.DataSource = tbldata;
cbo.DataTextField = "FullName";
cbo.DataValueField = "GUID";
cbo.DataBind();
Or implement 'Format' event like this:
DataRow r = ((DataRowView)e.ListItem).Row;
e.Value = r[ "FirstName" ] + " - " + r[ "LastName" ];
Have a property in your class that is the concat of Name and Surname. And bind the DataTextField to this property.
In case you are binding it to a DataTable, you can add a new column to the DataTable whose values are concat of Name and Surname and bind it to the combo.
Have a look at calculated columns using the Expression property of DataColumn.
You can register the combo binding event and iterate the items, setting each item text to the desired fields using the combobox item data item.