I have a grid which has 4 bands and the columns in band[3] are not arranged in order as in its parent bands. My primary requirement is to arrange the Band[3] columns.
This is a part of "Export to Excel" code. The grids are already displayed in the form.
I have tried the below approach -
private UltraGrid rearrangeCol(UltraGrid grid)
{
int bandCount = grid.DisplayLayout.Bands.Count;
int band = 0;
List<String> colPos = new List<string>();
for (int i = grid.DisplayLayout.Bands[band].Columns["EndurEndInv"].Index; i < grid.DisplayLayout.Bands[band].Columns.Count; i++)
{
colPos.Add(grid.DisplayLayout.Bands[band].Columns[i].Key);
}
band++;
while (band < bandCount)
{
int initialColPos = grid.DisplayLayout.Bands[band].Columns["EndurEndInv"].Index;
for (int i = initialColPos, j = 0; i < grid.DisplayLayout.Bands[band].Columns.Count && j < colPos.Count; i++, j++)
{
if(!grid.DisplayLayout.Bands[band].Columns[i].Key.Equals(colPos[j], StringComparison.InvariantCulture))
{
grid.DisplayLayout.Bands[band].Override.AllowColSwapping = Infragistics.Win.UltraWinGrid.AllowColSwapping.WithinBand;
int xcngPos = grid.DisplayLayout.Bands[band].Columns[colPos[j]].Index;
grid.DisplayLayout.Bands[band].Columns.Insert(i, "Temp");
grid.DisplayLayout.Bands[band].Columns[xcngPos + 1].Swap(grid.DisplayLayout.Bands[band].Columns[i]);
grid.DisplayLayout.Bands[band].Columns.Remove("Temp");
grid.DisplayLayout.Bands[band].Override.AllowColSwapping = Infragistics.Win.UltraWinGrid.AllowColSwapping.Default;
}
else
continue;
}
band++;
};
return grid;
}
Actually, nothing happens when I use SWAP, the keys, index remains same.
Is there any better approach?
Yes, to rearrange to position of colums you need to work on
grid.DisplayLayout.Bands[index].Columns[colName].Header.VisiblePosition = newPos;
no need to swap the column positions, just rearrange the Header position.
This code below has not been tested and I write as an example
private UltraGrid rearrangeCol(UltraGrid grid)
{
int bandCount = grid.DisplayLayout.Bands.Count;
int band = 1;
UltraGridBand b0 = grid.DisplayLayout.Bands[0];
while (band < bandCount)
{
UltraGridBand b = grid.DisplayLayout.Bands[band]
for (int i = b0.Columns["EndurEndInv"].Index; i < b0.Columns.Count; i++)
{
string colKey = b0.Columns[i].Key;
if(b.Columns.Exists(colKey))
b.Columns[colKey].Header.VisiblePosition =
b0.Columns[colKey].Header.VisiblePosition;
}
band++;
}
return grid;
}
Related
This is my code:
Private void TAbleconvertion () {
// Table alignment issue
for (int TableCount = 1; TableCount <= wdDocConv.Tables.Count; TableCount++) {
if (wdDocConv.Tables[TableCount] != null) {
Microsoft.Office.Interop.Word.Table tbl = wdDocConv.Tables[TableCount];
tbl.AllowAutoFit = true;
Range r = tbl.Range;
Cells cells = r.Cells;
for (int count = 1; count <= cells.Count; count++) {
// Resizing width - but not working when this code move to windows server
int size = 100 / cells.Count;
Cell cell = cells[count];
cell.PreferredWidth = size;
cells.PreferredWidth = size;
Marshal.ReleaseComObject(cell);
}
}
}
}
how to compare two selected rows in a dataGridView?
1.- I need to know how to detect the two selected rows
2.- Compare the two selected rows
3.- highlight the differences "cells"
I've tried this but unfortunately I am lost.....
DataTable src1 = dataGridView1.DataSource as DataTable; //THIS IS PROBABLY NOT NEEDED
DataTable src2 = dataGridView1.DataSource as DataTable;
int index1 = 0;
for (int i = 0; i < src1.Rows.Count; i++)
{
var row1 = src1.Rows[i].ItemArray;
var row2 = src2.Rows[i].ItemArray;
for (int j = 0; j < row1.Length; j++)
{
if (!row1[j].ToString().Equals(row2[j].ToString()))
{
dataGridView1.Rows[i].Cells[j].Style.BackColor = Color.Red;
dataGridView1.Rows[i].Cells[j].Style.BackColor = Color.Red;
}
}
}
Actually your code is not good i will answer your questions :
1.- I need to know how to detect the two selected rows
dataGridView1.SelectedRows
2.- Compare the two selected rows it should look to something similar to follow :
for (int i = 0; i < dataGridView1.SelectedRows.Count-1; i++)
{
for (int j = 0; j < dataGridView1.SelectedRows.rows[i].Cells.Count; j++)
{
if(dataGridView1.SelectedRows.rows[i].Cells[j].value.Equals(dataGridView1.SelectedRows.rows[i+1].Cells[j].value))
{
dataGridView1.SelectedRows.Rows[i].Cells[j].Style.BackColor = Color.Red;
dataGridView1.SelectedRows.Rows[i+1].Cells[j].Style.BackColor = Color.Red;
}
}
}
3.- highlight the differences "cells"
dataGridView1.SelectedRows.Rows[i].Cells[j].Style.BackColor = Color.Red;
dataGridView1.SelectedRows.Rows[i+1].Cells[j].Style.BackColor = Color.Red;
I have procedure that programmatically adding list to datagridview. For Example:
public List<Color_INFO> addrowtocolors()
{
List<Color_INFO> result = dal.GetColor();
for (int i = 0; i < list.Count; i++)
{
var index = grdColors.Rows.Add();
grdColors.Rows[index].Cells["Code"].Value = result[i].Code.ToString();
grdColors.Rows[index].Cells["Desc"].Value = result[i].Desc.ToString();
}
return null;
}
But when I call it is adding 3 same rows to datagridview , and in list i have only one.
I know that I can use dataset option , but that not fit for my needs.
Thanx.
As per my comment... I've also removed the return value as it seemed strange to just return null. (or even the proposed list based on your function name).
public void addrowtocolors()
{
for (int i = 0; i < result.Count; i++)
{
var index = grdColors.Rows.Add();
grdColors.Rows[index].Cells["Code"].Value = result[i].Code.ToString();
grdColors.Rows[index].Cells["Desc"].Value = result[i].Desc.ToString();
}
}
or
public void addrowtocolors()
{
for (int i = 0; i < list.Count; i++)
{
var index = grdColors.Rows.Add();
grdColors.Rows[index].Cells["Code"].Value = list[i].Code.ToString();
grdColors.Rows[index].Cells["Desc"].Value = list[i].Desc.ToString();
}
}
How can I locate an ui-element by Its location (row and column)?
UIElement FindByCell1(Grid g, int row, int col)
{
var childs = g.Children.Cast<UIElement>()
return childs.Where(x => Grid.GetRow(x) == row && Grid.GetColumn(x) == col).FirstOrDefault();
}
If there may be some elements in the same cell:
IEnumerable<UIElement> FindByCell(Grid g, int row, int col)
{
var childs = g.Children.Cast<UIElement>()
return childs.Where(x => Grid.GetRow(x) == row && Grid.GetColumn(x) == col);
}
In your case, This way - working directly with the elements in the UI - its very very not recommended, and its the extreme opposite of MVVM.
Great answer. My implementation (Generating array of Buttons in code then want indexed array thereof):
Button[,] Buttons = new Button[8, endTimePlus1 - startTime];
var buttons = controlGrid.Children.Cast<Button>();
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < endTimePlus1 - startTime; j++)
{
Buttons[i,j] = buttons.Where(x => Grid.GetRow(x) == j && Grid.GetColumn(x) == i).FirstOrDefault();
}
}
I'm using multi header columns (GroupFields) in WebDataGrid for my project. Is there a way to get the depth of header rows, i.e.. header row count. Consider the depth more than two, i.e.. a sub field can also be a groupfield and contain multiple child fields. So is there a way to compute the total header row count dynamically. I tried recursion, but got stuck since the loop won't stop after recursive fn and continue with the old count. Here is my code, any help is greatly appreciated.
protected void MultiColheader(object sender, EventArgs e)
{
List<GridField> unhiddenColumns = (from GridField p in LookupGrid.Columns
where !p.Hidden
select p).ToList<GridField>();
int count = 0;
int[] ar = new int[unhiddenColumns.Count];
for (int i = 0; i < unhiddenColumns.Count; i++)
{
if (LookupGrid.Columns[i].GetType() == typeof(GroupField))
{
count = 1;
GroupField groupfield = (GroupField)LookupGrid.Columns[i];
ar[i] = _HrowCount(groupfield, count);
}
count = 0;
}
Response.Write(ar.Max());
}
public int _HrowCount(GroupField gf, int count)
{
for (int k = 0; k < gf.Columns.Count; k++)
{
if (gf.Columns[k].GetType() == typeof(GroupField))
{
GroupField gpf = (GroupField)gf.Columns[k];
count = count + 1;
_HrowCount(gpf, count);
}
}
return count;
}
You can use a recursive function similar to:
private int RecursiveIteration(GridFieldCollection field)
{
int count = 0;
for (int i = 0; i < field.Count ; i++)
{
if (field[i].GetType() == typeof(GroupField))
{
count += RecursiveIteration((field[i] as GroupField).Columns);
}
count++;
}
return count;
}
The function takes a column collection as the argument, so in order to get the total column count:
int colCount = RecursiveIteration(WebDataGrid1.Columns);