how to move auto generated columns to left side? - c#

i have gridview and i am adding rows at runtime using datatable as datasource.but i want to add delete command field to grid view.I add it and run it.But i got delete button at left position.I want to change that delete button to last of gridview.Means after autogenerated columns.Please help me.Thanks in advance

This posting has what you need to know to put your generated columns in the leftmost part:
Move AutoGenerate Columns at LeftMost Part of the GridView Columns

You can change the order by doing the following :-
YourDataColumn.SetOrdinal(0);
This allows you to move the column to to nay position.
Hope this helps.

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e){
GridViewRow row = e.Row;
// Intitialize TableCell list
List<TableCell> columns = new List<TableCell>();
foreach (DataControlField column in GridView1.Columns)
{
if (row.Cells.Count > 0)
{
//Get the first Cell /Column
TableCell cell = row.Cells[0];
// Then Remove it after
row.Cells.Remove(cell);
//And Add it to the List Collections
columns.Add(cell);
}
}
// Add cells
row.Cells.AddRange(columns.ToArray());
}

Related

Insert data into Datagridview from comboboxes and textboxes to a specific row

So basically what I want to do is to select data from the combo boxes, and write something in the textboxes, and when I press the button all these data will fill the proper places of the datagridview.
I only found codes that fill up rows that are next to each other but I would have to write some logic for them to fill specific rows or don't fill them on certain values
private void fill_Click(object sender, EventArgs e)
{
table.Rows.Add(combo1.Text, combo2.Text, combo3.Text, text1.Text);
dataGridView1.DataSource = table;
}
This code only does the trick if these rows and values are supposed to be next to each other. But in my case, they don't.
How can I refer to a specific row that I want to fill?
It appears you are adding a "new" ROW to the table, so I am not sure what you mean by “specific” row? I assume you mean a “specific column” in a new row? If that is the case then this should work…
DataRow dr = table.NewRow();
// by column Name...
dr["Name"] = combo1.Text;
// or by index
int colIndex = 3;
dr[colIndex] = text1.Text;
// then add the row;
table.Rows.Add(dr);

Is it possible to disable alternating rows for a single column in a DataGridView?

I'm trying to add a column to an already populated DataGridView but I need this column cells to be all the same color since it is the sum of all the columns on each row.
I add the column this way:
dt.Columns.Add("Acumulable", typeof(float));
dt.Columns["Acumulable"].Expression = "Enero+Febrero+Marzo+Abril+Mayo+Junio+Julio+Agosto+Septiembre+Octubre+Noviembre+Diciembre";
And I set the color like this:
DGVGastosVariables.Columns["Acumulable"].DefaultCellStyle.BackColor = ColorTranslator.FromHtml("#3498DB");
It works fine since it does change the color of the cells but it keeps the alternating row pattern.
Is there a way to disable the alternating rows for that column in particular?
Thanks.
Try this. Subscribe your DataGridView to CellFormatting event:
private void DataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == /* Acumulable column index */)
{
e.CellStyle.BackColor = ColorTranslator.FromHtml("#3498DB");
}
}
Also you can use CellPainting event.

set 1st row of datagrid to a different color

I have a Datagrid where a add items using grid.Items.Insert(0, row);
I always want the last row I insert to be displayed first on Grid.
I also want the 1st row to have different color from others. For this purpose I am using LoadingRow event :
private void dataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
dataGrid.RowBackground = new SolidColorBrush(Colors.AntiqueWhite);//set all rows to default color
e.Row.Background = new SolidColorBrush(Colors.CornflowerBlue); //set current=>1st row row to BLue
}
However every row added is getting colored CornflowerBlue without previous added rows changed to AntiqueWhite.
I am missing something obvious, would really appreciate any guidance.
You need to loop through the rows in the datagridview and then compare values of columns 7 and 10 on each row (example columns).
Try this:
foreach (DataGridViewRow row in vendorsDataGridView.Rows)
if (Convert.ToInt32(row.Cells[7].Value) < Convert.ToInt32(row.Cells[10].Value))
{
row.DefaultCellStyle.BackColor = Color.Red;
}
Use Insert instead of Add. This way you can specify where you want to add the new record. To add a new record on top of the grid, just use Insert(0).
DataGridView1.Rows.Insert(0) // -- this will add a new record on top, always
DataGridView1.Rows(0).Cells(n).Value = "abc" // --this will set the value at n column.

Unable to select multiple rows on infragistics ultrawingrid with a check box column

I am having below code that successfully adds a check box column to the ultrawingrid, my problem is that when I make a selection by checking a check box on the Select column the count of selected rows of ultrawingrid is not updated and it still shows the count as zero, and also I want to know how to enable multi checkbox selection i.e multiple rows selected...
Below is the code...
private void grdPayVis_InitializeLayout(object sender,InitializeLayoutEventArgs e)
var gridBand = grdPayVis.DisplayLayout.Bands[0];
if(!gridBand.Columns.Exists("Select"))
gridBand.Columns.Add("Select", "Select");
gridBand.Columns["Select"].Header.VisiblePosition = 0;
gridBand.Columns["Select"].Hidden = false;
gridBand.Columns["Select"].Style = Infragistics.Win.UltraWinGrid.ColumnStyle.CheckBox;
gridBand.Columns["Select"].AutoSizeMode = ColumnAutoSizeMode.AllRowsInBand;
gridBand.Columns["Select"].CellClickAction = CellClickAction.Edit;
}
I am not sure what you actually is trying to achieve. When you set CellClickAction in your Select column to Edit, and you click on any cell in this column the grid will select the cell and not the row. Grid has Selected property which exposes three collections - rows, columns and cells. In your case you are changing the selected cells and do not change the selected rows collection. If you need to select rows you need to set CellClickAction in your Select column to RowSelect. If you need in the same time to shange the checkbox state of the Select column you may handle AfterSelectChange event of the grid like this:
private void grdPayVis_AfterSelectChange(object sender, AfterSelectChangeEventArgs e)
{
if (e.Type == typeof(UltraGridRow))
{
foreach (UltraGridRow row in this.grdPayVis.Selected.Rows)
{
row.Cells["Select"].Value = true; // Or some value appropriate to your scenario
}
this.grdPayVis.UpdateData();
}
By default, the grid allows you to select many cells, rows or columns. However, when there is a cell in edit mode you cannot select any other cells. So again, as you have set CellClickAction to Edit when you click any cell in Select column it enters edit mode and no more cells could be selected before you exit edit mode.
You can add a new column to your datatable AFTER reading it from the database
ds = new DataSet("PayCharge");
ds= obj.GetData();
DataColumn dc = new DataColumn("Select", typeof(boolean));
dc.DefaultValue = false;
ds.Tables[0].Columns.Add(dc);
grdVisPay.SetDataBinding(ds, "PayCharge");
.....
Now the first table in your dataset has an unbound column named Select and when you set that as your datasource you will be able to manipulate as you have already done. But this time whenever you touch the checkbox the value True/False will be set in the underlying datatable. When you need to discover what are the checked rows you work with the datasource, not with the grid. For example
void buttonSave_Click(object sender, EventArgs e)
{
DataSet ds = grdVisPay.DataSource as DataSet;
DataRows[] selectedRows = ds.Tables[0].Select("Select = True");
foreach(DataRow row in selectedRows)
{
... do whatever you need with the selected row...
}
}

moving columns in gridview between template fields and bound fields

I have a gridview (GridviewProduct) in that i Have a Template field column Link button as Remove From Cart and and another Template Field column next to it as Quantity and other three Fields are boundField like ProductName, ProductPrice and ProductBrand. Which Looks as Follows:
I want to move the quantity column to the end of the gridview at the right hand side. when i use this code it gives me an error:
Insertion index was out of range
var Quantity = GridViewProduct.Columns[1];
GridViewProduct.Columns.RemoveAt(1);
GridViewProduct.Columns.Insert(5, Quantity);
please help.
your requirement can be achieved in Gridview's RowCreated event
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
List<TableCell> columns = new List<TableCell>();
//Get the first Cell /Column
TableCell cell = row.Cells[1];
// Then Remove it after
row.Cells.Remove(cell);
//And Add it to the List Collections
columns.Add(cell);
// Add cells
row.Cells.AddRange(columns.ToArray());
}
If you have 5 columns and remove one of them, you have 4 left. The index of a new last column would than be 4 (since it is zero-based).
GridViewProduct.Columns.Insert(4, Quantity);
I am not sure if this removes all problems, but at least this error should go...

Categories

Resources