Table with dynamic number of columns to show - c#

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

Related

Best approach to set status count to ASP Grid?

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.

ASP.Net - My gridview is too tall when it has very few rows

I am using a GridView.
If my SQL Statement only returns one row, my gridview has one single row that is too tall.
If my SQL Statement returns only a few rows, my gridview has a few rows that are each too tall, but not nearly as tall as when only one row is returned.
If my SQL Statement returns a lot of rows, they are more normal-sized and there is no problem.
Why might my Gridview Rows change height like this based upon the quantity of rows returned?
You need to remove the Height attribute and value from your Gridview.
Example
<asp:GridView ID="GridView1" runat="server" Height="505px" ...
Should be
<asp:GridView ID="GridView1" runat="server" ...
Alternatively, remove the CSSClass from your gridview (or remove height from the CSS Class)
References
WebControl.Height MSDN
Gridview MSDN
Similar Problem

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";

Calculate Total Value In Gridview In ASP.net C#

I am using a website which contains a gridview for view details of Product details... It contains columns like name,area,phnoe no,quantity,price,total.... now I want to calculate total value for that I have to multiply the columns quantity and Price and also put that answer to total column in grid... How shall I do this?
There are three(may be more) ways to accomplish this.
First is query database and show result on grid
To do this : Query like
select name,area,phone,quantity,price,quantity * price as Total from YOUR_TABLE
Then bind it into your datagrid
Second is before binding your source which is here datatable into the grid,loop for this table for your sum.
To do this: You have datatable but not you have column named Total this time.
Now you need to add a new column as Total into the datatable.
DataColumn totalColumn = new DataColumn("Total");
totalColumn.Expression = "Quantity * Price";
totalColumn.DataType = //double,integer
dataTable.Columns.AddAt(totalColumn, 0);
All we have done above is generating a computed column
Third,use javascript for cells to assemble your specific values to sum,but this time you need to find your datagrid then your cells in which you should prefer using a javascript framework like jquery.I do not know any reference point for this,but First and second options will be much easier to do
Best Regards
Myra
Its simple yaar,
if the row index is 0 and your column name in price means ,the following is the way to getvalues,
GridViewID.Rows[0].cells["Price"].ToString()
It depends on your requirement/ wish, you can use Javascript to calculate total on client side if you are not fetching data from database.
You can also do it in code behind but it would be better to use JQuery to fetch the data and calculate it on client side.
but i have to it in code behind only
Ok then. You need to a template column to your GridView
<asp:TemplateField>
<ItemTemplate>
<asp:Literal ID="litTotal" runat="server" />
</ItemTemplate>
</asp:TemplateField>
Subscribe to the RowDataBound Event.
protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == RowType.DataRow)
{
YourObject _item = (YourObject)e.Row.DataItem; // use quickwatch to figure out how to cast into your desired type
Literal _litTotal = (Literal)e.Row.FindControl("litTotal");
_litTotal.Text = _item.Quantity * _item.Price;
}
}

Populating a GridView from a DataTable

I am new ASP.NET and I have never used a GridView or DataGrid, but I cannot find helpful examples online on how to do what I need. I need some data to be displayed on a page and I know that I should use a GridView, but I cannot seem to get anything to display.
I have the following DataSet:
DataSet ds = new DataSet("MyTables");
ds.Tables.Add("Users");
ds.Tables["Users"].Columns.Add("ID");
ds.Tables["Users"].Columns.Add("Name");
ds.Tables["Users"].Columns.Add("Email");
ds.Tables["Users"].Rows.Add(0,"Ace","ace#example.com");
ds.Tables["Users"].Rows.Add(1,"Biff","biff#example.com");
ds.Tables["Users"].Rows.Add(2,"Chuck","chuck#example.com");
ds.Tables["Users"].Rows.Add(3,"Dick","dick#example.com");
myGrid.DataSource = ds.Tables["Users"].DefaultView;
myGrid.DataBind();
Heres is my ASP.NET:
<asp:GridView ID="myGrid" runat="server">
</asp:GridView>
I think you're adding columns to the wrong table. Try something like this:
DataSet ds = new DataSet("MyTables");
ds.Tables.Add("Users");
DataTable userTable = ds.Tables["Users"];
userTable.Columns.Add("ID");
userTable.Columns.Add("Name");
userTable.Columns.Add("Email");
userTable.Rows.Add(0,"Ace","ace#example.com)";
userTable.Rows.Add(1,"Biff","biff#example.com)";
userTable.Rows.Add(2,"Chuck","chuck#example.com)";
userTable.Rows.Add(2,"Dick","dick#example.com)";
myGrid.DataSource = userTable;
myGrid.DataBind();
I'm thinking that you're getting a object null reference error. What's happening is that you're creating columns in a table called 'add users' where there is no table called 'add users'. If you just change the three lines that you have ds.Tables["Add Users"] and change that to just ds.Tables["Users"] everything should work out just fine.
Hope this works for you.
You are referencing two completely different tables. You create the Table USERS then add columns to a table called ADD USERS then add rows to the Users table. Nothing will show up when you bind this.
Change this code
ds.Tables["Add Users"].Columns.Add("ID");
ds.Tables["Add Users"].Columns.Add("Name");
ds.Tables["Add Users"].Columns.Add("Email");
to this
ds.Tables["Users"].Columns.Add("ID");
ds.Tables["Users"].Columns.Add("Name");
ds.Tables["Users"].Columns.Add("Email");

Categories

Resources