Best approach to set status count to ASP Grid? - c#

I need to show the user in a grid the count of establishments that has each active status in the application. I already have the query but obviously it returns me 2 columns, one with the status name and one with the count. Should I use something like Pivot Columns in SQL Server and set the columns names and properties on the fly using row[0] or there is a better approach for what I need to do?
P.S.
I am using Obout Grid to display the data.
<obout:Grid ID="gridStatus" runat="server" FolderStyle="~/obout/grid/styles/style_6"
AllowAddingRecords="False" AutoGenerateColumns="False" AllowColumnReordering="True"
PageSize="10" ShowTotalNumberOfPages="True" AllowManualPaging="True" AllowFiltering="True"
AllowMultiRecordSelection="false"
PageSizeOptions="10,20,30,40,50,100,500,999">
<Columns>
<obout:Column DataField="name" HeaderText="Status" />
<obout:Column DataField="statusTotal" HeaderText="Total"/>
</Columns>
<ScrollingSettings ScrollWidth="100%" />
<FilteringSettings FilterLinksPosition="TopAndBottom" FilterPosition="Top" />
</obout:Grid>
But I can't use that format, the columns has to be each status.

You can either pivot results of your query in backend SQL or, if it becomes to complicated - retrieve your 2-column data into .NET code and build a DataTable programmaticaly (create number of columns in the new DataTable according to number of rows in the result, name the columns according the "name" field and fill it with one row of data according to "statusTotal" field).
Then in your obout grid set AutoGenerateColumns="True" and do not specify <Columns></Columns>. This way grid will generate columns according to the datasource.

Related

asp GridView add data to a certain column (after TemplateField) with Boundfield?

I have a asp:GridView wich in code behind is asigned to a List with Objects.
gwMyGridview.DataSource = test; // test is a list with some objects (two fields)
gwTract.DataBind();
I can add buttons and stuff to the columns, but my data (two columns) are always the last two columns. I know BoundField can be used, but I still get two columns in the end.
How can i do this: column1 = data1, column2 = button, column3 = data.
Now it looks like this: button, data, data
Thanks in advance!
Don't let the GridView to auto generate the columns. To do this, follow these steps:
1- Click on the GridView's Tasks (little arrow on top of the GridView when you click on it) and click on Edit Columns, and uncheck the "Auto-Generate fields" field, or from the Markup set the AutoGenerateColumns property to false
2 - Add your columns manually to the GridView, by either adding them in the previous dialog (Fields) or in the markup, in the exact order you want them to a appear in your GridView:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="data1" HeaderText="Data1" />
<asp:TemplateField HeaderText="CustomColumn"></asp:TemplateField>
<asp:BoundField DataField="data2" HeaderText="Data2" />
</Columns>
</asp:GridView>
After that you can bind normally.

Table with dynamic number of columns to show

I have a data table with data like "insurance name, plantype, premium...." for each row.
So on my front end I have to show like below:
Insurance Name HealthNet Harvard UniCare
Plan Type HMO PPO HMO
Premium 100 150 200
So sometimes I may have only two columns to show HealthNet and Harvard. Sometimes more than three. How to use repeater in this case to make it dynamic based on data table count?
Thanks
Sample:
aspx markup as shown below:
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="true">
</asp:GridView>
In codebehind simply do this:
gv.DataSource = yourDataTable; // doesn't matter how many columns are there, it will handle it automatically
gv.DataBind();
This will help how to pivot a data table:
http://geekswithblogs.net/dotNETvinz/archive/2009/05/10/pivot-data-in-gridview---a-generic-pivot-method-with.aspx

DetailsView binds to columns in database and asp:bound columns resulting in duplicate columns shown?

I have a DetailsView that has a Fields element and some bound columns. The DetailsView is bound to a SqlDataSource. Code on dev and in Environment B is exactly the same, but on dev, I am seeing the columns from the database as well as columns that are bound, so it results in a duplicate columns shown. In Environment B, this is not happening, so I am wondering if it is a data issue, but I am not sure where to start or look?
<asp:DetailsView id="dv" runat="server" DataSourceID="sql" DataKeyNames="Id" OnDataBound="dv_DataBound" OnItemInserted="dv_ItemInserted" OnModeChanged="dv_ModeChanged" HeaderText="Add New" AutoGenerateInsertButton="true">
<Fields>
<asp:BoundField HeaderText="Name" DataField="sName" />
</Fields>
</asp:DetailsView>
The above DetailsView will generate a form that displays label sName with a textbox next to it and another label Name with a TextBox next to it, but the exact same code in Environment B only generates the label Name with a TextBox next to it. This has me thinking it is a data issue, but I can't pinpoint where to start looking. The SqlDataSource.SelectCommand is just a stored proc that is similar to:
SELECT * FROM TABLE_1
Does the DetailsView has something like AutoGenerateColums in gridview?

Get data for DataGridView from something else than properties

I have a DataGridView with a DataSource. The DataGridView auto-generates columns for each property. I want to change this, so that it gets the data for the columns in a different way.
How can I determine how the DataGridView gets the columns and values from the items in the datasource?
It probably uses reflection, do you want to intercept and modify that behavior really?
usually when the default automatic column generation is not enough I disable it and create the columns from code either statically or parsing a config file which could be extended afterwards so to have new columns or differently rendered ones without rebuilding the whole project.
I guess this all depends on what you are looking to do. If you just want to bind to specific columns, and not auto-generate the columns based off of the properties in the DataSource object, then you would do the following:
<asp:GridView ID="GridViewID" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
</Columns>
</asp:GridView>
With "AutoGenerateColumns" set to false, and specific columns set, the GridView will only bind to the properties, in the "DataSource" object, that are tagged in the "DataField" property of the specified columns.
Now, if you are looking to alter these data values, or specifically modify the data as it is being bound, then you would want to attach to the "RowDataBound" event. That way, when the data is bound you can alter it as it attaches to each row.
With the DataGridView.VirtualMode property, you can supply your own datastore:
Virtual mode is designed for use with very large stores of data. When
the VirtualMode property is true, you create a DataGridView with a set
number of rows and columns and then handle the CellValueNeeded event
to populate the cells. Virtual mode requires implementation of an
underlying data cache to handle the population, editing, and deletion
of DataGridView cells based on actions of the user. For more
information about implementing virtual mode, see How to: Implement
Virtual Mode in the Windows Forms DataGridView Control.

Can i show message of Record not found in asp.net Grid?

I have made a grid view in asp.net, when there is no record, grid is not shown, i want that if there is no record, grid must be shown, but there must be a message, record not found with gridview headers available. I mean gridview must be there, but instead of rows there should be a message in it, record not found, i have not used any code for fetching data in string, i just used datasource, please help.
The GridView has an EmptyDataText property, where if there is no record found it will be displayed.
EmptyDataText="Record Not Found"
If you want to show the header of GridView, then you can make the DataTable have all the columns that are in your DataSource and add an Empty Row and bind that DataTable if your original DataSource does not contain data.
You can display the custom text and format too.
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="true"
emptydatatext="No data in the data source."
runat="server">
<emptydatarowstyle backcolor="LightBlue"
forecolor="Red"/>
</asp:gridview>
Try this
GridView1.EmptyDataText = "Record not found";

Categories

Resources