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...
Related
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.
I have a GridView with data. The first column on this GridView is the "SELECT" column.
If the user clicks on SELECT, it highlights the entire row.
I have an event on this click action:
protected void gvShows_SelectedIndexChanged(object sender, EventArgs e)
{
}
Basically what I want to do is for each SELECTED row, I want to extract the values for the columns:
dataSource
showId
episodeId
**Here's where I'm having problems:
string dataSource = "";
int showId = 0;
int episodeId = 0;
foreach (DataRow row in gvShows.Rows)
if (the row is selected/highlighted then...)
{
dataSource = the value under column "dataSrouce" for this ROW.
showId = the value under column "showId" for this ROW.
episodeId = the value under column "episodeId" for this ROW.
}
Can anyone give me a hand with this?
I believe you will want to use SelectedIndexChanging (and not SelectedIndexChanged) because that will give you access to the new selected row index. If the first cell is the button, the next three should give you the values you need:
protected void gvShows_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
GridViewRow r = gvShows.Rows[e.NewSelectedIndex];
string dataSource = r.Cells[1].Text;
int showId = Convert.ToInt32(r.Cells[2].Text);
int episodeId = Convert.ToInt32(r.Cells[3].Text);
}
Edit:
I wanted to add that you can use the SelectedIndexChanged if you didn't want to use SelectedIndexChanging:
protected void gvShows_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow r = gvShows.Rows[gvShows.SelectedIndex];
string dataSource = r.Cells[1].Text;
int showId = Convert.ToInt32(r.Cells[2].Text);
int episodeId = Convert.ToInt32(r.Cells[3].Text);
}
And if you are working with a database, you can add a datakey to the gridview, allowing you to pull the primary key for the record you want to work on.
Add the following to your gridview:
DataKeyNames="showId"
And then you can access this value from the codebehind:
int showId = Convert.ToInt32(gvShows.DataKeys[gvShows.SelectedIndex].Value);
You need to rely on the "selected/highlighted" rows' attributes. Find any attribute that could determine if it is a selected row.
EventArgs e should have a Row property that represents the selected row. Within there, you have access to each of the columns (called Cells). You get into each particular cell using indexing. So, if ShowId is in column 3, here's how you'd access it:
e.Row.Cells[2]; (remember, it's 0-based indexing in C#).
First to get ShowId, try e.Row.Cells[2].Text; That might get you what you need.
If that doesn't work, try this:
Now, within Cells[2], you have access to Controls, which again is a list of controls in that cell. Run your code, and put a breakpoint inside of SelectedIndexChanged. Then in your immediate window, type the code above, and hover your mouse over Cells[2] to open up the items in there. Hover again on Controls, and look at what's in there.
Usually there's a LiteralControl, then your control with ShowId, then another LiteralControl.
Validate that, and then you'll be able to access it like e.Row.Cells[2].Controls[1].Text or something like that.
I have a DataGridView, which has two columns and several rows. The first column has the names of some attributes of an object and the second one should be filled with values by the user.
How can I get the second column of a selected row automatically in editmode independent of the column, which is entered to select the row?
This means, if a row is selected it should be immediately possible to enter some values into the second column of that specific row, even if the row is selected by entering the first column of the row.
By now, the first column has the property DataGridRowColumns.ReadOnly = true and the second one has DataGridViewColumn.ReadOnly = false. The grid has the following properties: DataGridView.MultiSelect = false and DataGridView.SelectionMode = FullRowSelect.
You can try with this code
protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.EditItem)
{
DataRowView dataItem1 = (DataRowView)e.Item.DataItem;
var result = (string)dataItem1.Row["YourColumnName"];
......
} }
I have some n no of rows in my GridView. Those items are categorized into some categories.
For example first 10 rows are categorized into a category and second 7 rows are categorized into second category.
There is a column named category in the Binding DataTable. Basing on that column the gridview has to be divided into categories.
Grouping in my sense is Background color of the category has to be changed for particular category.
You could create a CSS class for each of the categories that would set the background color that you want. Then set the CSS class for each data row in your GridView in the RowDataBound event.
protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.CssClass = ((MyDataClass) e.Row.DataItem).Category;
}
}
Try using the templates of the ListView instead to avail of this grouping ability.
ListView Grouping by Data Field
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());
}