Loop inside the gridview on devexpress - c#

I am new on devexpress,so I have a simple question here.what is the equal of the this code on devexpres gridview.
here is the my code
for (int i = 0; i <gridView1.RowCount; i++)
{
string Name = gridView1.Rows[i].["ColumName"].toString();
}
I looked at the a method something gridView1.Rows[i] but ,not available.

Take a look at the documentation for Traversing Rows in the DevExpress support center. Looks like this would work:
for (int i = 0; i < gridView1.VisibleRowCount; i++)
{
DataRow row = gridView1.GetDataRow(i);
string name = row["ColumnName"].ToString();
}

You can loop through like this
for (int i = 0; i < gridView1.VisibleRowCount; i++)
{
int currentRowHandle = gridView1.GetVisibleRowHandle(i);
string value = grid.GetCellValue(rowHandle,grid.Columns["Column Name"]);
}

Related

How do I bind a non database column value to repeater label using asp.net

I have a little challenge that I can't get around. I have a result from the code below.
if (Request.Cookies["ProductRecord"] != null)
{
s = Convert.ToString(Request.Cookies["ProductRecord"].Value);
string[] strArr = s.Split('|');
for (int i = 0; i < strArr.Length; i++)
{
t = Convert.ToString(strArr[i].ToString());
string[] strArr1 = t.Split(',');
for (int j = 0; j < strArr1.Length; j++)
{
a[j] = strArr1[j].ToString();
}
SingleCount = (Convert.ToDecimal(a[1].ToString()) * Convert.ToDecimal(a[3].ToString()));
}
}
How do I bind the results of SingleCount to a label inside a repeater.
RepeaterId.DataSource = SingleCount is the closest i have thought of but its not working.
I'm making some assumptions based upon your code so I may be off-base on what you're trying to achieve, but couldn't you take SingleCount and add it to a list of decimals and then bind that list to the DataSource of your repeater?

Add row to specific column by header name/text

I have a column named "HeaderText_Name" and in this column, I want to add a row that has the text "Row" under that column.
I attempted to write some pseudo-code for it
databaseGridView.Rows["HeaderText_Name"].Add("Row");
If I do
databaseGridView.Rows.Add("Row");
It adds it to the first column no matter what. I also can't do something like
databaseGridView.Rows.Add("","","Row");
Then it adds blanks to the first two columns and I also don't know how which index the column is. So I would be more helpful if it was by Name or text.
Here is the actual code I have so far..
for (int i = 0; i < completeInfoMatches.Count; i++) {
databaseGridView.Rows.Add();
databaseGridView.Rows[0].Cells[e.Node.Text].Value = completeInfoMatches[i].Groups[1].ToString();
}
Now, completeInfoMatches has more than 1 match because it's regex. How can I change the 0 to make it work?
UPDATE
int currentRowIndex = 0;
databaseGridView.Columns.Add(e.Node.Text.Replace(" ", string.Empty), e.Node.Text);
for (int i = 0; i < completeInfoMatches.Count; i++) {
databaseGridView.Rows.Add();
databaseGridView.Rows[currentRowIndex].Cells[e.Node.Text.Replace(" ", string.Empty)].Value = completeInfoMatches[i].Groups[1].ToString();
currentRowIndex = currentRowIndex + 1;
}
I'm getting a lot of extra blank rows because of Row.Add
Try something like this:
foreach(int i = 0; i < completeInfoMatches.Count; i++){
var index = databaseGridView.Rows.Add();
databaseGridView.Rows[index].Cells[e.Node.Text].Value = completeInfoMatches[i].Groups[1].ToString();
}
With the help of #Forlani ...
int currentRowIndex = 0;
int actualRowIndex = databaseGridView.Rows.Count;
for (int i = 0; i < completeInfoMatches.Count; i++) {
var index = 0;
if (actualRowIndex < completeInfoMatches.Count) {
index = databaseGridView.Rows.Add();
}
databaseGridView.Rows[currentRowIndex].Cells[e.Node.Text.Replace(" ", string.Empty)].Value = completeInfoMatches[i].Groups[1].ToString();
currentRowIndex = currentRowIndex + 1;
}

Getting only the last row in Datagrid with datagrid.items[index]

I am trying to run a loop on a datagrid having five rows, but I am only getting the last row in the datagrid.
for (int i = 0; i < mydataGrid.Items.Count; i++)
{
var newrow = (My_DTO)mydataGrid.Items[i];
MessageBox.Show(newrow.FirstName.ToString());
}
Try using this. This should help you get the row :
for (int i = 0; i < dataGrid.Items.Count; i++)
{
DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(i);
}
Each time you call MessageBox.Show(newrow.Firstname.ToString()); you'll be overwriting the previous name.
Try:
String names = "";
for (int i = 0; i < mydataGrid.Items.Count; i++)
{
var newrow = (My_DTO)mydataGrid.Items[i];
names += newrow.FirstName.ToString());
}
MessageBox.Show(names);
That should just show the names one after the other with no spaces, but I'm not sure how you want to display the names, so that part is up to you.

Read single cell in a DataGridView as a string

I'm reading all the cells in a DataGridView, but on each of them I want to compare the first 3 elements of the cell as a string, and turn it into a blank cell if they match.
So far I've tried this
for (int z = 0; z < dataGridView7.Rows.Count; z++)
{
for (int i = 0; i < dataGridView7.Columns.Count; i++)
{
string caixa = dataGridView7[z, i].Value.ToString(); //problem right here
string caixaselecta = caixa.Substring(0, 3);
if (caixaselecta == "0 *")
{
dataGridView7[z, i].Value = "";
}
}
}
The for cycles are correctly done, and the matches too. I think the problem is identifying the current cell being analyzed, and turn it into a string.
I've tried similar ways to reach the current cell, but either I get a error, or the string caixa isn't correctly defined.
Any hints?
You are specifying the index in wrong order. DataGridView indexer is defined as
public DataGridViewCell this[
int columnIndex,
int rowIndex
] { get; set; }
-Referenced from MSDN documentation
so you need to swap the z and i in indexer.
for (int z = 0; z < dataGridView7.Rows.Count; z++)
{
for (int i = 0; i < dataGridView7.Columns.Count; i++)
{
string caixa = dataGridView7[i, z].Value.ToString();
....
....
....
}
}

Get Cell Value from a DataTable in C#

Here is a DataTable dt, which has lots of data.
I want to get the specific Cell Value from the DataTable, say Cell[i,j]. Where,
i -> Rows and j -> Columns. I will iterate i,j's value with two forloops.
But I can't figure out how I can call a cell by its index.
Here's the code:
for (i = 0; i <= dt.Rows.Count - 1; i++)
{
for (j = 0; j <= dt.Columns.Count - 1; j++)
{
var cell = dt.Rows[i][j];
xlWorkSheet.Cells[i + 1, j + 1] = cell;
}
}
The DataRow has also an indexer:
Object cellValue = dt.Rows[i][j];
But i would prefer the strongly typed Field extension method which also supports nullable types:
int number = dt.Rows[i].Field<int>(j);
or even more readable and less error-prone with the name of the column:
double otherNumber = dt.Rows[i].Field<double>("DoubleColumn");
You probably need to reference it from the Rowsrather than as a cell:
var cellValue = dt.Rows[i][j];
You can iterate DataTable like this:
private void button1_Click(object sender, EventArgs e)
{
for(int i = 0; i< dt.Rows.Count;i++)
for (int j = 0; j <dt.Columns.Count ; j++)
{
object o = dt.Rows[i].ItemArray[j];
//if you want to get the string
//string s = o = dt.Rows[i].ItemArray[j].ToString();
}
}
Depending on the type of the data in the DataTable cell, you can cast the object to whatever you want.
To get cell column name as well as cell value :
List<JObject> dataList = new List<JObject>();
for (int i = 0; i < dataTable.Rows.Count; i++)
{
JObject eachRowObj = new JObject();
for (int j = 0; j < dataTable.Columns.Count; j++)
{
string key = Convert.ToString(dataTable.Columns[j]);
string value = Convert.ToString(dataTable.Rows[i].ItemArray[j]);
eachRowObj.Add(key, value);
}
dataList.Add(eachRowObj);
}
You can call the indexer directly on the datatable variable as well:
var cellValue = dt[i].ColumnName
If I have understood your question correctly you want to display one particular cell of your populated datatable? This what I used to display the given cell in my DataGrid.
var s = dataGridView2.Rows[i].Cells[j].Value;
txt_Country.Text = s.ToString();
Hope this helps

Categories