I want to do it with Devexpress extension (gridview) :
string dataInCell = dataGridView1.Rows[i].Cells[j].Value.ToString();
Like :
gridView1.Rows[i].Cells[j]
If you are trying to get the value of a cell in a specefic row, here is how :
a. If you want the value of a cell of the focused row :
view.GetFocusedRowCellValue("fieldName");
b. If you want the cell value of a row knowing his handle :
view.GetRowCellValue(rowHandle, "fieldName");
Good luck
try this
for (int i = 0; i < gridView.RowCount; i++)
{
dataInCell = Convert.ToString(gridView.GetRowCellValue(i, "FieldName"));
}
To get a spescific row you can use these commands.
GridView.GetDataRow(rowHandle)
or
GridView.GetRow(rowHandle)
but if you want to modify a range of cells, it is usually better to go directly at the datasource
You presumably have set a datasource on the grid? If so, use the datasource and access it via its datasource index.
Using row handles could causes issues when the grid is sorted. I recommend...
int dataIndex = gridView.GetDataSourceRowIndex(rowHandle);
var myData = myDataSource[dataIndex];
Provided you're using a generic collection there is no casting involved and this handles grouping and sorting. Of course what is displayed and what is the data may not be the same thing. E.g. If the data is an enumeration. You would display the displayname for this but the value in the datasource is the enum. Normally I need the underlying value instead of the displayed text.
you can use below code:
dataGridView1.GetRowValues(dataGridView1.FocusedRowIndex,"column1-name","column2-name",...);
with this you can get value with row index that focused on it and select with field`s name,return value type of object and you cast to int,string and ...
such :
string id=(string)dataGridView1.GetRowValues(dataGridView1.FocusedRowIndex,"column1-name");
but it depends to type column1-name
string dataInCell = ((DataRowView)gridControl1.MainView.GetRow(i)).Row.ItemArray[j].ToString();
I think you are looking for this:
string str = gridView1.GetRowCellValue(Convert.ToInt32("ROW_NUMBER"), "COLUMN_NAME").ToString();
you should use GetRowCellValue
string cellValue;
cellValue = gridView1.GetRowCellValue(2, "ID").ToString();
All above step you can, but keep in mind that null values conversation will be thrown an error, so before access it do Convert.IsDBNull().
I think it's the best code for get row column field.
string name= gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "name").ToString(),
You can get the value of grid cell using
string cellValue = gvGrid.GetRowValues(visibleIndex, "FieldName").ToString();
where visibleIndex is the row's index. You can just loop like this
if (gvGrid.VisibleRowCount > 0)
{
for (int index = 0; index < gvGrid.VisibleRowCount; index++)
{
string cellValue = gvGrid.GetRowValues(index, "FieldName").ToString();
}
}
Related
I've tried to display the values of properties from a list of class on the table of MS Word like this:
public void CreateTable<T>(IList<T> list)
{
if (list != null)
{
var props = typeof(T).GetProperties();
Range tableLoc = WdDoc.Range();
var table = WdDoc.Tables.Add(tableLoc, list.Count, props.Length);
for (int i = 0; i < props.Length; i++)
{
table.Columns[i + 1].Select();
if (props[i].PropertyType == typeof(double))
{
WdApp.Selection.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight;
}
else
{
WdApp.Selection.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
}
}
//...
}
}
I tried to automatically set the text alignment of columns according to fill-in property type.
However, only the first row of each column can be set as the correct alignment I assign.
Other rows will always be set as the latest alignment of for loop.
Why did this happen? Did I miss any snippet?
Also, what are the differences between
WdApp.Selection.Paragraphs.Format.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
and
WdApp.Selection.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
Thanks!
The problem comes from using Selection.Range in this specific instance of working with the Word object model. When working with columns you need to use
WdApp.Selection.ParagraphFormat.Alignment
Background
Selection affects exactly what is selected - the entire column.
Selection.Range affects the Range of the selection. In the case of a Column, Selection.Range cannot be the entire column. A Range in Word must be a contiguous run of text, but a column is NOT contiguous. So Selection.Range returns only that part of the selection that is contiguous: the first cell (row). That explains why only the first row of the table is affected by your current code.
While a selected column appears to be contiguous this is just a convenience for the user. Under the covers, everything in the rows between the cells in one column and the next breaks up the text run.
If you're familiar with HTML think of how a table is defined in HTML. The way Word Open XML defines a table works on the same principle. The concept looks something like this:
<table><row><cell><cell></row><row><cell><cell><row></table>
The rows and their content are contiguous.
I am new to c# and using windows forms.
I have a dataTable as shown in screenshot, I want to get a value of specific cell in button_Text column based on Button_Name column value and save it in a string. I know I can use this code :
string s = DataTable.Rows[2][1];
But I do not want to use it because I do not want to use numbers between brackets to index the cell value but instead I want to use the a Button_Name column value and button_Text column name to index the cell value.
For example:
string s = DataTable.Rows[ButtonUC1_3][Button_Text]; // but it gives error : "Argument1: can not convert from string to int"
I hope it is clear enough. Anyone knows how to achieve this? Thank you
Use LINQ to DataSet there you can use column names
string value = dataTable.AsEnumerable()
.Where((row) => row.Field<string>("button_Name").Equals("ButtonUC_1"))
.Select((row) => row.Field<string>("button_Text"))
.FirstOrDefault();
Here Rows Must be Int formet,But Columns string allowed,
below one is error
string s = DataTable.Rows["ButtonUC1_3"]["Button_Text"];
the correct formet is,
string s = DataTable.Rows[2]["Button_Text"];
It will give the cell value,.
DataTable Rows don't have names, they're indexed only by number. If you want to access the rows through the names you specified, you need to use your own mapping, e.g. var rowMapping = new Dictionary<string,int>() { { ButtonUC1_3, 0}, ... } and then access the DataTable like this DataTable.Rows[rowMapping[ButtonUC1_3]][Button_Text].
Obviously, this is not the only way how to do it, you could define an enum, integer constants, etc. It all depends how many rows you have and how you use your DataTable.
I have a datagridview that contains list of subjects populated from Subject table from database.Columns include
Select(checkbox),
SubjectId,
SubjectName,
SubjectGroup.
Now I want if a user Selects on any of the desired rows, the corresponding SubjectId's should be added to a List. I have made and inserted into the desired table in the database.
The problem is that the new column of checkboxes I have added to this datagridview is not being detected.
My code is:
foreach (DataGridViewRow row in gvSubjectsOpted.Rows)
{
if (Convert.ToBoolean(gvSubjectsOpted.SelectedRows[0].Cells["SelectId"].Value=true))
{
olist.Add(gvSubjectsOpted.SelectedRows[0].Cells["SubjectId"].Value.ToString());
}
}
Late to the party. I had the same issue with trying to get the checkbox column by name, use the index instead. Here is a linq example assuming the checkbox is column 0 and the stored values for TrueValue and FalseVale are true and false respectively.
var checkedRows = from DataGridViewRow r in gvSubjectsOpted.Rows
where Convert.ToBoolean(r.Cells[0].Value) == true
select r;
foreach (var row in checkedRows)
{
olist.Add(row.Cells["SubjectId"].Value.ToString());
}
I realise this is an old post but I came across it and didn't think it was really answered in an efficient way so I thought I would add my method.
I have a similar block in my windows app. I read the values from the grid when the user clicks a button, and I want to know which rows they checked. As the checkboxes are in Cell 0 and the data I want is in Cell 1, I use the following code. Note the cast: it is important as it allows us the use the Where clause and therefore just a single line of code to get the collection of data. I could use the name of the cells instead of magic index numbers but then it would not fit your app so I put numbers instead (you should use names)
var checkedRows = dataGridView
.Rows
.Cast<DataGridViewRow>()
.Where(x => x.Cells[0].Value.ToString() == "1")
.Select(x => x.Cells[1]);
Note that this will give you an IEnumerable of type DataGridViewCell. If you want you can either add something like .Value.ToString() to the select or do this when you use your collection.
You question is similar to another SO question.
Check the answer of this Datagridview checkboxcolumn value and functionality.
Try this
foreach(GridViewRow r in gvSubjectsOpted.Rows)
{
GridViewCheckBoxColumn c = r.cells[0].Controls[0] as GridViewCheckBoxColumn;
if(c.Checked)
{
//Do something.
}
}
private void button1_Click(object sender, EventArgs e)
{
string subjId;
List<string> lines = new List<string>();
for (int i = 0; i < gvSubjectsList.Rows.Count; i++)
{
bool Ischecked =Convert.ToBoolean(gvSubjectsList.Rows[i].Cells["Select"].Value);
if (Ischecked == true)
{
subjId = gvSubjectsList.Rows[i].Cells["SubjectId"].Value.ToString();
lines.Add(subjId);
}
}
comboBox1.DataSource = lines;
}
//the most important thing is to set 'true' and 'false' values against newly added checkboxcolumn instead of '0' and '1'...that is,
CBColumn.FalseValue = "false";
CBColumn.TrueValue = "true";
I use a DevexpressGridView to display all TOPIC (id,title,content)
<dx:ASPxGridView ID="gv" runat="server"
OnSelectionChanged="gv_SelectionChanged" >
I have grid_SelectionChanged event:
protected void gv_SelectionChanged(object sender, EventArgs e)
{
int id= selected row...???; //how can I get the value of selected row
string sql = "select * from TOPIC where idTOPIC="+id;
DataTable topic = l.EXECUTEQUERYSQL(sql);
TextBox1.Text = topic.Rows[0][1].ToString();
}
...
It seems gv.SelectedRow method isn't exist in DevGridview.
As recommended, I've tried with FocusedRowIndex method, but I really dont know the right syntax to get the value of selected row.
Help!!!
Changing the selection is different from changing the focused row. See the documentation for Selection for the difference between the two.
You can use gv.GetSelectedFieldValues to get the rows which are selected.
var ids = gv.GetSelectedFieldValues("id");
foreach( var id in ids )
DoSomethingWithObject(id);
You should handle the FocusedRowChanged event if you're interested in the focused row.
You can use the FocusedRowIndex value to index the rows of gv.DataSource, for example:
DataTable ds = (DataTable)gv.DataSource;
var id = ds.Rows[gv.FocusedRowIndex]["id"];
or you can use var id = gv.GetRowValues(gv.FocusedRowIndex, "id").
I've found my answere here after a long time searching google:
http://www.devexpress.com/Support/Center/Question/Details/Q347704
Use the ASPxGridView.GetSelectedFieldValues method get selected row values on the server side.
You can also get selected data row as
int rowHandle = gridView1.FocusedRowHandle;
if (rowHandle != DevExpress.XtraGrid.GridControl.InvalidRowHandle)
{
return this.gridView1.GetDataRow(rowHandle);
}
This would return DataRow
Please note this is when I am using Devexpress gridControl in WinForms
If you want to get only ID field value you can use this
int id = Convert.ToInt32(gv.GetRowValues(gv.FocusedRowIndex, "ID").ToString());
if you have an object you can use this
Personels selectedPersonel = gv.GetRow(gv.FocusedRowIndex) as Personels;
and get value method is
int ID = selectedPersonel.ID;
I need to select a value from dropdown based on index. It is a super easy question. cannot find the property:
I though doing something like:
dll.Items[index]
But still do not know how to get value for this index.
You can use dll.Items.FindByIndex(index); or dll.Items.FindByValue(val); according to your needs.
This loops through all items of an ASP combobox:
DataTable dt = (DataTable)comboBox1.DataSource;
for(int i = 0; i < dt.Rows.Count; ++i)
{
string displayText = dt.Rows[i][comboBox1.DisplayMember].ToString();
string valueItem = dt.Rows[i][comboBox1.ValueMember].ToString();
}
can be done like below
dll.Items[index].value
btw, FindByIndex, is like not available