I have a datatable with sorted column values and when I bind them to the gridview I'm not able to see the same order in the gridview like I have in the datatable.
Sorting event is also disable in gridview.
Can anyone please provide suggestions.
HTML:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" ShowHeaderWhenEmpty="true" EmptyDataText="No Records Found" OnRowDataBound="GridView1_RowDataBound" AllowSorting="true" OnSorting="GridView1_Sorting"
OnPageIndexChanging="GridView1_PageIndexChanging" AllowPaging="true" CellPadding="1" PageSize="200" AutoPostBack="true" AllowCustomPaging="true" ShowHeader="true"
CellSpacing="1" BackColor="#e7e7e8" BorderColor="#e7e7e8" GridLines="Both" CssClass="GridViewStyleB" Font-Names="Calibri" Font-Size="10pt">
<PagerSettings Mode="NumericFirstLast" PageButtonCount="4" FirstPageText="First" LastPageText="Last" Position="Top" Visible="true" />
<PagerStyle BackColor="#e7e7e8" ForeColor="Black" HorizontalAlign="left" />
<HeaderStyle CssClass="RowStyle" ForeColor="Black" BackColor="#a9a9a9" Font-Underline="false" HorizontalAlign="Left" />
<RowStyle CssClass="RowStyle" HorizontalAlign="Left" BorderColor="#E7E7E8" ForeColor="Black" />
<AlternatingRowStyle CssClass="AlternatingRowStyle" BorderColor="#E7E7E8" ForeColor="Black" BackColor="#e7e7e8" />
<EmptyDataRowStyle HorizontalAlign="Center" />
</asp:GridView>
Code:
DataTable dtColumns = FromDataTable();
GridView1.DataSource = dtColumns;
GridView1.DataBind();
dtColumns datatable have sorted data and I when I bind them to the gridview the order changes.
If you insert a breakpoint at GridView1.DataSource = dtColumns, and inspect the dtColumns object, can you confirm the sort order of the rows? What is the underlying query used to select the data? If it's from a single table, what data type is the primary key?
I think if you check those things, you will find your answer, but if not, you should update your post to include that stuff :)
EDIT
Here is something you can try to force the sort, after you perform the databinding:
GridView1.Sort("column name", System.Web.UI.WebControls.SortDirection.Descending);
Related
I have a gridview on my web page.
I am able to populate the Grid using C# in the behind code.
Here is a sample screen of 2 columns and 2 rows:
What I don't understand is why there Gridlines are not showing on the rows of data when I have it set on the gridview properties to show "both".
<asp:GridView id="gvappts"
runat="server"
AllowPaging="True"
AllowSorting="True"
AutoGenerateColumns="False"
BorderColor="#EFE6F7"
ForeColor="#003399"
Width="100%"
Font-Size="Small"
GridLines="Both"
BorderStyle="Solid"
BorderWidth="1px"
CellPadding="3">
<Columns>
<asp:BoundField DataField="apptid" HeaderText="Appt #" />
<asp:BoundField DataField="name" HeaderText="Name" />
</Columns>
</asp:GridView>
What am I missing?
Please try the below code to your row border..
<RowStyle HorizontalAlign="Center" BackColor="White" BorderColor="#0000CC"
BorderStyle="Solid" BorderWidth="2px"/>
And also try the same to columns.
All of the comments on this post are correct to some extent which is, you mention that the issue must rely in the styles, after going line by line in my style sheets I found the culprit style and was able to get the grid-lines to show.
I have a web app I'm trying to write that will get the percentages of supplies left in a printer. and display that in a friendly 1-100 percent number. to get that number i must multiply what is left by 100 then divide that by the total possible.
I have not been able to take the data that is coming from a sql server table and modify it before it is rendered into the data gridview. Any help would be appreciated.
I've already tried in the code behind to convert it to integer with no luck. Here is my code.
ASPX
<asp:GridView ID="GridView2" runat="server" AllowSorting="True"
AutoGenerateColumns="False" BackColor="#DEBA84" BorderColor="#DEBA84"
BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" OnRowDataBound = "changeToPercent"
DataSourceID="SqlDataSource1" EnableModelValidation="True">
<Columns>
<asp:BoundField DataField="p_ipaddress" HeaderText="p_ipaddress"
SortExpression="p_ipaddress" />
<asp:BoundField DataField="s_prtmarkerssuppliesdescription"
HeaderText="s_prtmarkerssuppliesdescription"
SortExpression="s_prtmarkerssuppliesdescription" />
<asp:BoundField DataField="s_prtMarkersSuppliesLevel"
HeaderText="s_prtMarkersSuppliesLevel"
SortExpression="s_prtMarkersSuppliesLevel" />
</Columns>
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:XrxDBCWWConnectionString %>"
SelectCommand="usp_SUPPLYPERCENTAGE" SelectCommandType="StoredProcedure"
CancelSelectOnNullParameter="False">
<SelectParameters>
<asp:ControlParameter ControlID="percentBox" DefaultValue="10"
Name="PCT_REQUESTED" PropertyName="Text" Type="Int32" />
<asp:Parameter DefaultValue="" Name="PRINTER_ID" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
I have no CS Code to use because everything i have tried has failed miserably
Try wiring up the GridView's RowDataBound event, where you would have access to the Row data, and can update them before it gets bound to the grid view.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx
void GridView2_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// Display the company name in italics.
e.Row.Cells[1].Text = "<i>" + e.Row.Cells[1].Text + "</i>";
}
}
Sorry about the premature post. I had given up entirely editing my code so instead i went to the source and looked at the stored procedure to see if i could make it work there. Just so happens that you can do Mathmatical function INSIDE your SELECT statement.
This is what i added in the select.
SELECT ((s_prtMarkersSuppliesLevel *100) / s.s_prtMarkersSuppliesMaxCapacity) as s_prtMarkersSuppliesLevel
The output is like this:
MyNameIsJohnSmithAnd
Imaperson
What I want is to display it in only one line
MyNameIsJohnSmithAndImaperson
My Aspx gridview code is:
<asp:GridView ID="GridView1" runat="server" BackColor="LightGoldenrodYellow"
BorderColor="Tan" BorderWidth="1px" CellPadding="5" Font-Names="Calibri"
Font-Size="Medium" Font-Underline="False" ForeColor="Black">
<RowStyle Wrap="False"/>
<EmptyDataRowStyle Wrap="False"/>
<FooterStyle BackColor="Tan" BorderColor="Black" BorderStyle="Solid" Wrap="False"/>
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue"
HorizontalAlign="Center" Wrap="False" />
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" Wrap="False"/>
<HeaderStyle BackColor="Tan" BorderStyle="Solid" Font-Bold="True" Wrap="False"/>
<EditRowStyle Wrap="False"/>
<AlternatingRowStyle BackColor="PaleGoldenrod" Wrap="False"/>
</asp:GridView>
I disabled all the wrap property to false in gridview. but the text still wraps.
You have to set the gridview's individual column's word wrap to False as well.
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" >
<ItemStyle Wrap="False" />
</asp:BoundField>
Try adding this event to your gridview.
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
}
}
Here is the reference.
Very simple in .Net Select your grid view (as in Design mode) in property window and follow this
RowStyle -->Font -->>Wrap=False
Thats done
You can do this by css.
#GridView1
{
white-space:nowrap;
}
My column header text was wrapping so I ended up needing a slightly different solution but similar to what user3578419 suggested.
In design view, I edited the 'ColumnHeadersDefaultCellStyle' property and changed the 'WrapMode' value to 'False'.
I have a dynamic gridview with data pulled from oracle database.
<asp:GridView ID="gvData" OnRowDataBound="gvData_RowDataBound" OnPreRender="gvData_PreRender" OnSelectedIndexChanged="gvData_SelectedIndexChanged" OnLoad="gvData_Load" OnRowCommand="gvData_RowCommand" runat="server" CellPadding="4" ForeColor="#333333" AllowPaging="True" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" DataSourceID="DS" AllowSorting="True" AutoGenerateSelectButton="True" PageSize="10" ShowFooter="True">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#CCCCCC" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
<asp:SqlDataSource ID="DS" runat="server">
</asp:SqlDataSource>
I am trying to write DELETE/UPDATE statements. I am having a problem with getting values from gridview and adding them to the parameters os SQL statements.
protected void gvData_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
DS.DeleteCommand =
"DELETE FROM TABLE " +
"WHERE PROBLEM_DEPTH_MD = :PROBLEM_DEPTH_MD";
"AND API = :API " +
"AND BUS_AREA_ID = :BUS_AREA_ID";
DS.DeleteParameters.Add(new Parameter("PROBLEM_DEPTH_MD", TypeCode.String, ??GridViewItemValue??));
}
I don't know how to get a value from the gridview. Any suggestions? Thanks!
First off you need to mention the DataKeyName to your gridview of your Primary key Column and then you can access the value of the Selected row of the Gridview like... e.Keys["DataKeyName"]
If you want to access a non key field, then it should be like... e.Values["FieldName"]
Mention DataKeyName like... <asp:GridView ID="gvData" DataKeyNames="PrimaryKeyColumn"
You will need to set the DataKeyNames for you GridView.
You can then use the GridViewDeleteEventArgs to get the Keys property. This will be a dictionary of Key/Value pairs which correspond to the DataKeyFields which you set. In essence it allows you to get your primary keys-- which is what you need to delete.
assuming primary keys are PROBLEM_DEPTH_MD, API, AND BUS_ARE_ID, you would set the DataKeyNames to be "PROBLEM_DEPTH_MD,API,AND BUS_ARE_ID".
You could then grab these keys in your delete event with e.Keys. If you loop over them you should be able to get the values to add your parameters.
Here is some more information on this:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.datakeynames.aspx
I am using checkboxs in a GridView. I need to determine the value in the cell in the 2nd column, if a checkbox has been checked.
I am using C# 2008 and ASP.net
<asp:GridView ID="gvOrders" runat="server" AutoGenerateColumns="False" CellPadding="4" GridLines="None" Width="100%" AllowPaging="True" PageSize="20"
onpageindexchanging="gvOrders_PageIndexChanging" ForeColor="#333333">
<Columns>
<asp:TemplateField HeaderText="VerifiedComplete" >
<ItemTemplate>
<asp:CheckBox ID="cbPOID" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="PurchaseOrderID" HeaderText="PurchaseOrderID" HtmlEncode="False" ></asp:BoundField>
<asp:BoundField DataField="VENDOR_ID" HeaderText="Vendor ID"></asp:BoundField>
<asp:BoundField DataField="VENDOR_NAME" HeaderText="Vendor Name"></asp:BoundField>
<asp:BoundField DataField="ITEM_DESC" HeaderText="Item Desc"></asp:BoundField>
<asp:BoundField DataField="SYS_DATE" HeaderText="System Date"></asp:BoundField>
</Columns>
<FooterStyle CssClass="GridFooter" BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle CssClass="GridPager" ForeColor="#333333" BackColor="#FFCC66" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<HeaderStyle CssClass="GridHeader" BackColor="#990000" Font-Bold="True" ForeColor="White" />
<RowStyle CssClass="GridItem" BackColor="#FFFBD6" ForeColor="#333333" />
<AlternatingRowStyle CssClass="GridAltItem" BackColor="White" />
</asp:GridView>
protected void btnDisable_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvr in gvOrders.Rows)
{
if (((CheckBox)gvr.FindControl("cbPOID")).Checked == true)
{
string strPrimaryid = gvr.Cells[2].ToString();
}
}
}
Another way to do this, to avoid accessing the cells of the GridView by index, is to convert the desired BoundField to a TemplateField in the designer. Then use the FindControl method to get the text value in that cell. By default, the designer will give the Label control of the new TemplateField a name like "Label1."
I would add some pictures to show the designer part, if that process were easier. At any rate, the aspx would change like so, which you could do manually too:
<asp:BoundField DataField="PurchaseOrderID" ...</asp:BoundField>
becomes
<asp:TemplateField HeaderText="PurchaseOrderID">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("PurchaseOrderID") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("PurchaseOrderID") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
Then you would make this simple code change:
string strPrimaryid = gvr.FindControl("Label1").Text;
However, I think a better way to do this is by utilizing the DataKeys features of the GridView control. The first step would be to set the DataKeyNames property of your GridView:
<asp:GridView ID="gvOrders" runat="server" DataKeyNames="PurchaseOrderID" ...>
Then, assuming your PurchaseOrderID column is of type int, you would change that same line to be:
int primaryid = (int)gvOrders.DataKeys[gvr.RowIndex].Value;