I have create a code for GrideViewServer.FooterRow to get selected value of dropdownlist in footer row(insert). Now I want to do same thing for edit row to get selected value from combobox, however they are not in footer row.
Here what I write for footer row,
string architecture = ((DropDownList)GridViewServer
.FooterRow.FindControl("DropDownArchitecture")).Text;
Now I want to write for edit row, how can I write it? Something like this?
string architecture = ((AjaxControlToolkit.ComboBox)GridViewServer
.EditRow.FindControl("ComboBox1")).Text;
Can't find options function for edit section.
You don't have only one EditRow, you have a collection of rows of this type, to find a control inside, you need to iterate using for or foreach:
foreach (GridViewRow r in GridViewServer.Rows)
{
if (r.RowType == DataControlRowType.DataRow)
{
string architecture = ((AjaxControlToolkit.ComboBox)r.FindControl("ComboBox1")).Text;
}
}
I found the answer. Here what I wrote,
((AjaxControlToolkit.ComboBox)GridViewServer.Rows[GridViewServer.EditIndex].FindControl("Combobox2")).Text;
Related
im using WPF in c#.
How to get all the value of the row selected from column M.
Image of listview
From ListView.SelectedItems Property,
foreach (var item in myListView.SelectedItems)
{
//do something with the values, maybe add to a list?
DataRowView drv = (DataRowView)lstNames.SelectedItem;
String valueOfItem = drv["NameAndScore"].ToString();
}
Basically replace "NameAndScore" by your column name.
Reference: Why I get “System.Data.DataRowView” instead of real values in my Listbox?
I'm currently trying to add data programatically onto a DataGridView, but it doesn't seem to be working.
What I have is an Array, which I fill from a text file:
public static string PathList = #"C:\Users\gbbb\Desktop\Pfade.txt";
_PathRows = System.IO.File.ReadAllLines(#PathList);
and I have a DataGridView with 4 Columns on which I add as many Rows as I have paths, so:
public void InitPathsTable()
{
TabelleBib.Rows.Add(_PathRows.Length);
//And here is where i want to add the Paths on Column Nr.4
}
Next what I need is a way to add all paths that I get (24) into the Column Nr.4,
one Path per Row.
But it seems to be nearly impossible for a beginner like me, so I am asking you.
This is method that will do that for you. Read comments (especially make sure you have added 4 columns to you DataGridView):
public void InitPathsTable()
{
int rowindex;
DataGridViewRow row;
foreach (var line in _PathRows)
{
rowindex = TabelleBib.Rows.Add(); //retrieve row index of newly added row
row = TabelleBib.Rows[rowindex]; //reference to new row
row.Cells[3].Value = line; //set value of 4th column to line. WARNING: TabelleBib has to have 4 columns added either from code or designer othwerwise here you will get exception
}
}
if you get any more problems, write a comment and I will come back to you :)
HI
How to get CurrentCell location in DataGridView. i need for when my load will load every comboboxes set above on datagridview current cell. and get same size. how can i do that?
You can get the zero-based index of the column that contains the currently selected cell by using the ColumnIndex property of the CurrentCell property:
if (myDataGridView.CurrentCell != null)
{
int columnIndex = myDataGridView.CurrentCell.ColumnIndex;
}
Or, you can get the column object itself that contains the currently selected cell by using the OwningColumn property:
if (myDataGridView.CurrentCell != null)
{
DataGridViewColumn columnObject = myDataGridView.CurrentCell.OwningColumn;
}
Note that I'm still not entirely sure if this is what you're asking for. My forms don't have columns, so I'm assuming that you mean the DataGridView. And your answer still doesn't really explain what exactly you mean by "location," but this should tell you everything you need to know about the column that contains the current cell.
I have a form with a DataGridView (of 3 columns) and a Button. Every time the user clicks on a button, I want the get the values stored in the 1st column of that row.
Here is the code I have:
private void myButton_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in ProductsGrid.Rows)
{
if (this.ProductsGrid.SelectedRows.Count == 1)
{
// get information of 1st column from the row
string value = this.ProductsGrid.SelectedRows[0].Cells[0].ToString();
}
}
}
However when I click on myButton, the this.ProductsGrid.SelectedRows.Count is 0. Also, how do I ensure that the user selects only one row and not multiple rows?
Does this code look right?
Set DataGridView.MultiSelect=false and DataGridView.SelectionMode = FullRowSelect. This will make it so the user can only select a single row at a time.
SelectedRows only returns the rows if the entire row is selected (you can turn on RowSelect on the datagridview if you want). The better option is to go with SelectedCells
private void myButton_Click(object sender, EventArgs e)
{
var cell = this.ProductsGrid.SelectedCells[0];
var row = this.ProductsGrid.Rows[cell.RowIndex];
string value = row.Cells[0].Value.ToString();
}
Well, you don't need to both iterate over all rows in your grid and access the collection of SelectedRows. If you skip iteratating and use the SelectedRows collection, then your problem is probably an incorrect SelectionMode:
The SelectionMode property must be set
to FullRowSelect or RowHeaderSelect
for the SelectedRows property to be
populated with selected rows.
(from MSDN)
You can reference the grid similar to an array:
ProductsGrid[ProductsGrid.SelectedColumns[0].Index, ProductsGrid.SelectedRows[0].Index].Value;
By selecting the indexes from the first index of the SelectedRowsCollection and SelectedColumnsCollection you'll grab the first value if multiple rows are selected.
You can lock the user to selecting only a single row by setting the MultiSelect property on the DataGridView. Alternatively you make the CellClick event perform:
ProductsGrid.ClearSelection();
ProductsGrid.Rows[e.RowIndex].Selected = true;
SelectedRows.Count returns the number of entire rows that are currently selected. You probably want to use SelectedCells.Count.
you can also use the .BoundItem
I'm using Excel Interop assemblies for my project,
if I want to use auto filter with then thats possible using
sheet.UsedRange.AutoFilter(1,SheetNames[1],Microsoft.Office.Interop.Excel.XlAutoFilterOperator.xlAnd,oMissing,false)
but how can I get the filtered rows ??
can anyone have idea??
Once you filtered the range, you can access the cells that pass the filter criteria by making use of the Range.SpecialCells method, passing in a valued of 'Excel.XlCellType.xlCellTypeVisible' in order to get the visible cells.
Based on your example code, above, accessing the visible cells should look something like this:
Excel.Range visibleCells = sheet.UsedRange.SpecialCells(
Excel.XlCellType.xlCellTypeVisible,
Type.Missing)
From there you can either access each cell in the visible range, via the 'Range.Cells' collection, or access each row, by first accessing the areas via the 'Range.Areas' collection and then iterating each row within the 'Rows' collection for each area. For example:
foreach (Excel.Range area in visibleCells.Areas)
{
foreach (Excel.Range row in area.Rows)
{
// Process each un-filtered, visible row here.
}
}
Hope this helps!
Mike
I used as mentioned below, similar to what Mike told,
foreach (Excel.Range area in visibleCells.Areas)
{
foreach(Excel.Range row in area.Rows)
{
int index = row.Row; // now index is the present Row index within the range
string test = Mysheet.Cells[index,4].Values //// Mysheet is my present working sheet. After this test will contain the values pointing to the values.cells[index,4]
}
}