I have a gridview which is totatly bounded by programatically .And i added a command field for selection .But it shows up on the first column .Bu i want to change its order to the last column. I tried to use Displayindex , but that column doesn't involve a headerrow.
Here is my gridview looks like
UserID UserName UserAddress
-------------------------------------------------
Select 1 testname testaddress
I just want to pass Select to the last column
UserID UserName UserAddress
-------------------------------------------------
1 testname testaddress Select
<asp:GridView ID="GridForUnits" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None" Style="text-align: left" Width="851px" OnRowDataBound="GridForUnits_RowDataBound"
OnSelectedIndexChanged="GridForUnits_SelectedIndexChanged1">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:CommandField ShowSelectButton="True" />
</Columns>
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>
Any Help Appreciate.
Thanks
I would recomend to not auto generate the columns, fill them manually, and then change the visible index of all of them , setting the select button the last one.
More explanation here
Other solution: move the TableCells around in the RowDataBound event:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
List<TableCell> cells = new List<TableCell>();
foreach (DataControlField column in GridView1.Columns)
{
// Retrieve first cell
TableCell cell = row.Cells[0];
// Remove cell
row.Cells.Remove(cell);
// Add cell to list
cells.Add(cell);
}
// Add cells
row.Cells.AddRange(cells.ToArray());
}
Link to source
Related
I have a gridview with template button in it each row
each button fires an Onclick event.
i need to track the row of the pressed button.
if i want , lets say colorize the background , how should i do it?
<asp:GridView ID="OrdersGridView" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:Button ID="DeleteOrder" runat="server" BackColor="Red" Font-Bold="True" ForeColor="White" Text="מחק" OnClick="DeleteOrder_Click" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="OrderHost" HeaderText="OrderName" />
<asp:BoundField DataField="OrderId" Visible="False" />
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
The event is empty.
protected void DeleteOrder_Click(object sender, EventArgs e)
{
}
Based from your comment, you just want to get the index of your GridView.
protected void DeleteOrder_Click(object sender, System.EventArgs e)
{
Button btn = (Button)sender;
GridViewRow row = (GridViewRow)btn.NamingContainer;
//Get the Row Index by calling row.RowIndex;
}
Hope it helps!
i have a gridview as:
<asp:GridView ID="gdvOpinions" runat="server" Width="100%" CellPadding="4"
ForeColor="#333333" GridLines="None" Height="100px" Visible="False"
AutoGenerateColumns="False" onrowcommand="gdvOpinions_RowCommand" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:ButtonField ButtonType="Button" CommandName="Confirm" Text="تائید" />
<asp:ButtonField ButtonType="Button" CommandName="Delete" Text="حذف" />
<asp:BoundField DataField="ID" HeaderText="ID" />
<asp:BoundField DataField="مقاله" HeaderText="مقاله" >
<ControlStyle Width="150px" />
</asp:BoundField>
<asp:BoundField DataField="نظر کاربر" HeaderText="نظر کاربر" >
<ControlStyle Width="250px" />
</asp:BoundField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
As you see, i have two buttonfield for every row, but when i click one of these buttons and want to get their row in
gdvOpinions_RowCommand
the
gdvOpinions.SelectedRow
returns null;
how should i find which row is selected?
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false"
OnRowCommand = "OnRowCommand">
<Columns>
<asp:ButtonField CommandName = "ButtonField" DataTextField = "CustomerID"
ButtonType = "Button"/>
</Columns>
</asp:GridView>
protected void OnRowCommand(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow gvRow = GridView1.Rows[index];
}
From what I understand out of your question, if you are trying to get the row for which the button was clicked, then in that case this might be the right thing to do :
GridViewRow gRow = (GridViewRow)((Control)e.CommandSource).NamingContainer;
Now here, this is for a control such as a Button or ImageButton. You might have to improvise on this for ButtonField.
You can also change the ButtonField to a simple TemplateField with a Button.
Hope this helps.
I am designing a search page where if the View/Edit link button is clicked, another page loads with more info on the subject, however; when the button is clicked, it does not throw the Gridview's RowCommand event. The Gridview is built dynamically based on search terms and comparing to a database through an SQLDataSource. Here is the html for the Gridview.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="RID" DataSourceID="Report_System" CellPadding="4" ForeColor="#333333" GridLines="None" OnDataBound="GridView1_DataBound" Width="739px" OnRowCommand="GridView1_RowCommand">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="RID" HeaderText="Report ID" InsertVisible="False" ReadOnly="True" SortExpression="RID" />
<asp:BoundField DataField="Rdate" HeaderText="Report Date" SortExpression="Rdate" />
<asp:BoundField DataField="Rtype" HeaderText="Type" SortExpression="Rtype" />
<asp:BoundField DataField="OID" HeaderText="Officer ID" SortExpression="OID" />
<asp:BoundField DataField="Ofirstname" HeaderText="Officer" SortExpression="Ofirstname" />
<asp:BoundField DataField="Pname" HeaderText="Student Reportee" SortExpression="Pname" />
<asp:ButtonField CommandName="View/Edit" Text="View/Edit" />
</Columns>
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>
Currently, the RowCommand event is SUPPOSED to simply post back to a label for testing purposes.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
//get row index
int row = Convert.ToInt32(e.CommandArgument);
errorLabel.Text = row.ToString();
}
I have made sure that ViewState is Enabled, the OnRowCommand is in the right place, and my PageLoad event is empty. I can't find any forum posts to help me beyond this. Any ideas?
Try adding a check on CommandName inside GridView1_RowCommand with an if statement, like this
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if(e.CommandName == "View/Edit")
{
//get row index
int row = Convert.ToInt32(e.CommandArgument);
errorLabel.Text = row.ToString();
}
}
I have a gridview control on my web form with 1 template field column.
And i am adding some bounded columns at run time from code behind.
And some times I've removed some of the previously added columns from code behind.
After removing any column gridview losses template column.
What is the cause behind this and how can i prevent template column with out setting EnableViewState="false".
Edit-1
.aspx page Code
<asp:GridView ID="grvSum" runat="server" Width="100%" GridLines="None" AllowPaging="True" ShowFooter="true"
PageSize="25" CellPadding="4" ForeColor="#333333" AutoGenerateColumns="false" AllowSorting="true" EnableViewState="false"
OnRowUpdating="grvSum_RowUpdating" OnPageIndexChanging="grvSum_PageIndexChanging" OnSorting="grvSum_Sorting"
OnRowDataBound="grvSum_RowDataBound" Font-Size="10px">
<Columns>
<asp:TemplateField >
<ItemTemplate>
<asp:LinkButton ID="dtype" runat="server" CommandName="update"
CssClass="lbl" Font-Underline="true" style="cursor:pointer;" Text="Details" >
</asp:LinkButton>
</ItemTemplate>
<ItemStyle Width="20px"/>
<FooterTemplate>
<asp:Label ID="lblFooter" runat="server" Text="Total"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<AlternatingRowStyle BackColor="White" HorizontalAlign="Center"/>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" HorizontalAlign="Center"/>
<HeaderStyle HorizontalAlign="Center" BackColor="#507CD1" Font-Bold="True" ForeColor="White" Height="30px" Wrap="true"/>
<PagerStyle HorizontalAlign="Right" CssClass="GridPager" />
<RowStyle BackColor="#EFF3FB" HorizontalAlign="Center"/>
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" CssClass="headerSortUp" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" CssClass="headerSortDown"/>
</asp:GridView>
Code for adding and removal of column
public void AddBoundedColumns(GridView grv, DataColumnCollection dtDataSourceColumns)
{
var gridBoundColumns = grv.Columns.OfType<BoundField>();
foreach (DataColumn col in dtDataSourceColumns)
{
//Check existence of column in gridview
if (gridBoundColumns.Any(bf => bf.DataField.Equals(col.ColumnName)) == false)
{
//Declare the bound field and allocate memory for the bound field.
BoundField bfield = new BoundField();
//Initalize the DataField value.
bfield.DataField = col.ColumnName;
//Initialize the HeaderText field value.
bfield.HeaderText = col.ColumnName;
bfield.SortExpression = col.ColumnName;
//Add the newly created bound field to the GridView.
grv.Columns.Add(bfield);
}
}
gridBoundColumns = grv.Columns.OfType<BoundField>();
int z = 0;
for (int x = 0; x < gridBoundColumns.Count(); x++)
{
BoundField c = gridBoundColumns.ElementAt(z);
if (!dtDataSourceColumns.Contains(c.HeaderText))
{
grv.Columns.Remove(c);
}
else
{
z++;
}
}
}
instead of grv.Columns.Remove(c);, try following while removing the column
grv.columns.RemoveAt(index); //"index" is the index of column you want to remove
I have the following piece of code. I did not define any boundfields in my gridview. I am retrieving data using sql queries in my aspx.cs file instead. Is it possible to adjust the width of each column 0, 1, 2? Are there any ways that I can look into? I have tried a lot of ways but it is still not working. Please help!
<asp:GridView ID="surgicalGridView" runat="server"
CaptionAlign="Top" HorizontalAlign="Justify"
DataKeyNames="id" onselectedindexchanged="surgicalGridView_SelectedIndexChanged"
ToolTip="Excel File Download Tool" CellPadding="4" ForeColor="#333333"
GridLines="None" Width="854px">
<RowStyle BackColor="#E3EAEB" />
<Columns>
<asp:CommandField ShowSelectButton="True" SelectText="Download"
ControlStyle-ForeColor="Blue">
<ControlStyle ForeColor="Blue"></ControlStyle>
</asp:CommandField>
</Columns>
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<AlternatingRowStyle BackColor="White" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>
<br />
You can do that on the OnRowDataBound event of the gridview.
protected void surgicalGridView_RowDataBound(object o, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].Width = new Unit("200px");
e.Row.Cells[1].Width = new Unit("400px");
// and so on
}
}
Add this to your Gridview Markup
<asp:GridView ...............................
onrowdatabound="surgicalGridView_RowDataBound"> // just add this event and execute the above code
</asp:GridView>
My solution is below. I have a grid that has 2 defined columns and the rest are bound dynamically. I don't know why setting the column with (e.Row.Cells[0].Width = new Unit("200px");) didn't work, but I found an alternative. Also, my grid has sorting enabled, therefore the linkbutton code.
const int FirstControl = 0;
const int GriDefinedFieldsCount = 2;
protected void grd_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
int col = 0;
foreach (DataColumn dc in SiteManager.Reports.ReportData.Columns)
{
if (dc.ColumnName == "Notes")
{
LinkButton lnk = (e.Row.Cells[col + GriDefinedFieldsCount].Controls[FirstControl] as LinkButton);
lnk.Width = Unit.Pixel(300);
}
col += 1;
}
}
As grid is rendered as table tr and dt so you can use the css class for the grid.
There you can set the width of your columns.
In classes your can use like td, td+td,td+td+td etc
According to the post
.NET Gridview themes examples
Check out these links.
http://icant.co.uk/csstablegallery/index.php?css=69#r69
http://mattberseth2.com/demo/ has lot of gridview customizations with code download.
Paging
Paging With Slider
Sorting with sort icons
Some more themes
http://mattberseth2.com/demo/Default.aspx?Name=A+YUI+DataTable+Styled+GridView&Filter=All
http://mattberseth.com/blog/2007/11/5_gridview_themes_based_on_goo.html
(source: mattberseth.com)