I have a gridview binded to a datasource.
<asp:GridView ID="DocumentReviewGrid" runat="server" AllowPaging="True" AllowSorting="True"
EnableModelValidation="True" Width="100%" BorderStyle="None"
CssClass="docnav_data" BorderWidth="1px" GridLines="None" DataSourceID="DocumentReviewDataSource"
HorizontalAlign="Left" OnRowDataBound="DocumentReviewGrid_RowDataBound"
OnRowCreated="DocumentReviewGrid_RowCreated" CellSpacing="5"
PageSize="20" OnPageIndexChanged="DocumentReviewGrid_PageIndexChanged">
<AlternatingRowStyle BackColor="#EBF2F9" BorderStyle="None" />
<FooterStyle HorizontalAlign="Left" />
<HeaderStyle BackColor="#E7E7E7" HorizontalAlign="Left" />
<PagerSettings Mode="NumericFirstLast" Position="Top" PageButtonCount="4" />
<PagerStyle HorizontalAlign="Center" />
</asp:GridView>
As you can see Autogenerated Column is set to true, and it must be kept like that. One of the column is a SQL bit value, so it's represented as checkbox. I would like to be able to edit the checkbox column only, without using "AutoGenerateEditButton" property. I would just like to:
be able to check/uncheck the checkbox (I am stuck here)
performing a single update using an external button
the other columns must be readonly
Autogenerated columns cannot be manipulated directly in pretty much anyway, so there is no simple way to do it. So what you could do is create a custom column, which will always come first before any auto generated columns (again, this behavior cannot be changed), and hide the auto generated bit column.
How to hide the column is described here. Essentially you cannot use Columns collection, so need to do this:
protected void DocumentReviewGrid_RowCreated(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[X].Visible = false; // hides the first column
}
Here X is the 0-based index of the column to hide.
And now to prepend the column just define it the way you want, leaving AutoGenerateColumns="true":
<asp:GridView ID="DocumentReviewGrid"...>
<Columns>
<asp:CheckBoxField HeaderText="Esclusione" DataField="Esclusione" />
</Columns>
</asp:GridView>
Admittedly this is quite hackish, but that will get you almost where you want - bool column displayed and editable.
Related
I have a grid view, in which I have 1 column as template field, while other 8 columns are dynamically created.
This template field has a checkbox, while trying to retrieve this checkbox in code-behind file, we get it as null.
When columns are not dynamically created, the checkbox is pretty fine and has a value in code behind.
Below is my code:
<asp:GridView ID="gridResultSet" runat="server" AutoGenerateColumns="false" AllowSorting="true"
OnRowCreated="GridResultSet_RowCreated" OnLoad="GridResultSet_Load" CssClass="reportGrid"
CellPadding="4" OnSorting="GridResultSet_Sorting" OnRowDataBound="GridResultSet_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Reclass">
<ItemTemplate>
<acesec:CheckBox ID="chkReclass" CssClass="CheckBoxListStyle" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle CssClass="gridrow" />
<AlternatingRowStyle CssClass="gridrow" />
<HeaderStyle CssClass="gridheader" />
</asp:GridView>
System.Web.UI.WebControls.CheckBox chbReclass = gridResultSet.Rows[i].FindControl("chkReclass") as System.Web.UI.WebControls.CheckBox;
Do I need to check something in order to access a template field while creation of dynamic columns?
Any body faced same kind of situation?
Pointers will be highly appreciated.
Regards
I thought this would be an easy one for me to figure out (or atl east find someone online who has) but I am running into issues.
I have the following code that creates my gridview:
<asp:GridView runat="server" ID="ContactsGrid" AutoGenerateColumns="False" DataSourceID="LinqContact"
CellPadding="4" ForeColor="#333333" GridLines="None" OnRowDeleting="ContactsGridView_RowDeleting" >
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="IConact_ID" Visible="false" ReadOnly="true" />
<asp:BoundField DataField="cFirstName" HeaderText="First Name" ReadOnly="True" />
<asp:CommandField HeaderText="Delete" ShowDeleteButton="True" />
</Columns>
</asp:GridView>
<asp:LinqDataSource ID="LinqContact" runat="server" ContextTypeName="TIPS.App_Data.TIPSDataContext" onselecting="LinqContact_Selecting" >
</asp:LinqDataSource>
Now on the c# side, I want to be able to pull the value from the first column (which is hidden) and use it to delete that specific record (with the onroedeleting event), but all the ways I found to pull the value, all come up null, like what would happen if I didn't have a LinqDatasource.
I have tried (and a slew of other that really didn't seem to be right, so not listed):
ContactsGrid.SelectedRow.Cells[0].Text;
ContactsGrid.Columns[0];
Thanks for any help!
Edit:
Ok, so I found that you can't get the value of a column this is hidden when you hide it using the grid. I did find a work around. If you hide the column using css instead, you can still access the colum.
<style type="text/css">
.hiddencol
{
display:none;
}
</style>
<asp:BoundField DataField="IContact_ID" ReadOnly="true" itemstyle-cssclass="hiddencol" />
I don't think that this is the prefered .net way. I found a reference to something called datakeynames which seems to be the proper way. I am going to dig into those next.
Edit #2:
I see that Maras and myself both came up with a solution for the hidden field, but I think I found the best one (cause its simple and built in).
In the gridview tag you can set the datakeynames attribute to your column (like a primary key, which is what I was storing in my hidden column) you can also store multiple columns.
<asp:GridView runat="server" ID="ContactsGrid" AutoGenerateColumns="False" DataSourceID="LinqContact" DataKeyNames="IContact_ID"
CellPadding="4" ForeColor="#333333" GridLines="None" OnRowDeleting="ContactsGridView_RowDeleting" >
you can then reference it with:
ContactsGrid.DataKeys[e.RowIndex].Value;
Try this:
void ContactsGrid_RowDeleting(Object sender, GridViewDeleteEventArgs e)
{
ContactsGrid.Rows[e.RowIndex].Cells[0];
}
See here for more details http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdeleting.aspx#Y200
.
Edit after author's comment:
If you set it visibility to 'false' then there is nothing your browser can send you back in postback. You can use hidden field like in the second example:
<asp:GridView runat="server" ID="gv" OnRowDeleting="gv_RowDeleting" AutoGenerateColumns="false" AutoGenerateDeleteButton="true">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="hf" runat="server" Value="<%# ((YourItemType)Container.DataItem).Id %>" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
HiddenField hf = (HiddenField) gv.Rows[e.RowIndex].Cells[0].FindControl("hf");
}
I have a simple data grid...
<asp:DataGrid AllowPaging="true" AllowCustomPaging="true" runat="server" ID="grid"
PageSize="20" AutoGenerateColumns="false" AllowSorting="true"
OnSortCommand="OnTableSort"
OnPageIndexChanged="grid_PageIndexChanged"
<Columns>
<asp:BoundColumn DataField="FullName" HeaderText="Name" sortexpression="FullName" />
...
On sort I'd like to change the Css class of the header column so that I can add an sort indicator using css. Does anyone know how I could do this in the code behind? I've Googled for an hour and not found anyone doing this.
protected void OnTableSort(object sender, DataGridSortCommandEventArgs e)
{
...
By the way I want to use DataGrid. I don't have time to change to a different control. Any help appreciated.
I have a gridview that I populate based on a query to a db. I'm trying to add sorting on my gridview but I cannot make my column headers clickable. I have allow sorting set to true, and I have my OnSorting event set. My columns are of a few different types. I know the code I need to have in my code behind, but I cannot click on the headers for some reason. Any help on what I'm missing would be appreciated.
<asp:GridView ID="Grid1" runat="server"
AutoGenerateColumns="False"
OnSelectedIndexChanging="Selected_Row_Changing"
DataKeyNames="ApplicationId"
AllowPaging="True"
OnPageIndexChanging="Grid1_PageIndexChanging"
AllowSorting="True"
OnSorting="Grid1_Sorting"
OnRowCreated="OnRowCreated"
OnRowCommand="Grid1_RowCommand"
OnRowDataBound="Grid1_RowDataBound">
<Columns>
<asp:templatefield ...>
<itemtemplate>
<asp:linkbutton .../>
</itemtemplate>
</asp:templatefield>
<asp:BoundField ... />
<asp:HyperLinkField ... />
<asp:ButtonField ... />
</Columns>
</asp:GridView>
You don't have set the SortExpression, have you?
For example:
<asp:boundfield datafield="CompanyName"
headertext="CompanyName"
headerstyle-wrap="false"
sortexpression="CompanyName"/>
Make sure you're not setting the Header Template but rather set the HeaderText attribute for the TemplateField
I have an ASP.NET GridView that just won't sort! I'm sure that I am missing something pretty obvious.
Page.aspx
<asp:GridView ID="TimeAwayGridView" runat="server" AutoGenerateSelectButton="False"
AutoGenerateEditButton="False" AutoGenerateDeleteButton="False" AllowPaging="False"
AllowSorting="True" CssClass="gridview" OnSorting="TimeAwayGridView_Sorting">
<Columns>
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
<asp:BoundField DataField="Hours" HeaderText="Hours" SortExpression="Hours" />
</Columns>
<EmptyDataTemplate>
There are currently no items in this table.
</EmptyDataTemplate>
</asp:GridView>
Page.aspx.cs
protected void TimeAwayGridView_Sorting(object sender, GridViewSortEventArgs e)
{
}
Asp.Net Datagrip provides you with sorting event, and name of the column that was clicked in GridViewSortEventArgs, but you have to provide you own sort implementation in TimeAwayGridView_Sorting function. Meaning you should sort your datasource and rebind the datagrid.