Bind Values to GridView - asp.net

I have a List with following values
List<Calculations> calcs = new List<Calculations>();
Calculations cal = new Calculations();
cal.TotalTotalC2 = Convert.ToInt32(reader["TotalTotalC2"].ToString());
cal.TotalTotalC3 = Convert.ToInt32(reader["TotalTotalC3"].ToString());
cal.TotalC1C4IOM = Convert.ToInt32(reader["TotalC1C4IOM"].ToString());
cal.TotalC1C4MDR = Convert.ToInt32(reader["TotalC1C4MDR"].ToString());
calcs.Add(cal);
i need to Bind these Numeric values in the following table format.left hand side plain text and right hand side the bound values.
Can some one plz tell me that how can i use GridView to bind in above format.

you have to write code in RowDataBound event , and check for row number in each iteration and based on the row number bind the column text of your custom list to the column, I don't think there is any auto function to bind column names to grid!!! let me know if you find something direct and easy

I had to dynamically generate a DataTable and then bind it to GridView
DataTable dt=new DataTable();
dt.Columns.Add("Title", typeof(string));
dt.Columns.Add("Count", typeof(int));
foreach (Calculations item in calcs )
{
dt.Rows.Add("Total ...", item.TotalTotalC2);
dt.Rows.Add("Total ...", item.TotalTotalC3);
}

Related

how can i get value from database into dropdown list?

dropdown list was automatically generating number sequence. after selecting value from the database it showing same as the auto generated dropdown list
e ddlDuration.DataSource = Enumerable.Range(1, 100);
ddlDuration.DataBind();
ddlDuration.Items.Insert(0, new ListItem("--Select Customer--", "0"));
this code I used for generating numbers.
ddlDuration.SelectedIndex = ddlDuration.Items.IndexOf(ddlDuration.Items.FindByValue(oList[0].Duration));
and this for reading data from the list. the value can get till the
oList[0].Duration.
Hi Raheez,
It may be happening because the value of oList[0].Duration is not in 1-100 range.
Or
Try this :
ddlDuration.SelectedIndex = ddlDuration.Items.IndexOf(ddlDuration.Items.FindByValue(oList[1].Duration.Trim()));

How to check specific column exist in datarow?

I came across situation,where columnname for datatable is dynamic.While fetching a data I want to check existence of column.
DataTable table = ds.Table["Sample1"]
if(table.Row.Count > 0)
{
foreach(DataRow dr in table.Rows )
{
if(dr.Table.Column.Contain("DateInfo"))
{
// store value in variable
// first approach
}
if(table.Column.Contain("DateInfo"))
{
// store value in variable
// second approach
}
}
}
Which one is best approach?
Will this be enough:
1st Approach: Which will simply check in an entire DataTable.
datatable.Columns.Contains("column")
2nd Approach: which will check for each row collection in DataTable
dr.Table.Columns.Contains("column")
3rd Approach: Which fetch each columns in DataColumnCollection object and then check if it contains the specific field or not.
DataColumnCollection columns = datatable.Columns;
if (columns.Contains(columnName))
So these all approaches are better in their own way. you can use whatever you find it better.
This is best one
dr.Table.Column.Contain("DateInfo")
foreach loop get single row at a time sometimes if any conditional is possible in this method

Formatting Data in DataSet

I created a data set from an xml file and attached it to a gridview. I would like to do how do i format the data in the dataset or from the gridview, for example, change the header text for a column or also show only certain items in my gridview.
Dim xmlDataSet As New DataSet
xmlDataSet.ReadXml(GlobalClass.GlobalUrl)
GridView1.DataSource = xmlDataSet
GridView1.DataBind()
Is there anyway how to go about manipulating the data inside a dataset? Thank you.
You can try with theses posts :
First definition : http://msdn.microsoft.com/en-us/library/zb0sdh0b.aspx
Note : it's very interessant to read theses links about methods and properties
link 1 : http://msdn.microsoft.com/fr-fr/library/system.data.dataset.aspx
link 2 : http://msdn.microsoft.com/fr-fr/library/system.data.dataset.tables.aspx
You can do this for renaming columns:
xmlDataSet.Tables[0].Columns["CurrentColumnName"].ColumnName = "NewColumnName";
Here is a way to hide or show columns:
xmlDataSet.Tables[0].Columns["ColumnName"].Visible = true;

Data formatting in GridView with AutoGenerateColumns true

I've got a GridView in ASP.NET 2.0 with AutoGenerateColumns set to true. It'll be bound at runtime to a DataSet with one of many possible schemas, and I'd rather not set up grids and columns for each possible schema.
Some of the columns in the grid will sometimes be floating point values. It seems the default number formatting turns 0.345 into 0.345000. Is there a way to change the default number format so it trims to a set number of decimals?
You could use strings in your schema instead of floating point for display purposes, and perform the formatting manually, something like this:
EDIT: Without LINQ, you can do the same thing by modifying rows in the schema:
// load source data
DataSet myData = GetDataSet();
// create column for formatted data.
myData.Tables["MyTable"].Columns.Add("AmountFormatted", typeof(string));
// apply formatting
foreach (DataRow dr in myData.Tables["MyTable"].Rows)
dr["AmountFormatted"] = string.Format("{0:0.###}", dr["Amount"]);
// remove source column and replace with formatted column
myData.Tables["MyTable"].Columns.Remove("Amount");
myData.Tables["MyTable"].Columns["AmountFormatted"].ColumnName = "Amount";
C#, LINQ-Based solution:
var theData = GetDataSchema1();
var dataSource = theData.Tables["MyTable"].Select().Select(dr =>
new { /* select only columns you want displayed, and apply formatting */
MyColumn1 = dr["MyColumn1"],
MyColumn2 = dr["MyColumn2"],
MyColumn3 = String.format("#.###", dr["MyColumn3"]),
MyColumn4 = dr["MyColumn4"],
MyColumn5 = dr["MyColumn5"]
}
);
MyGridView1.DataSource = dataSource;
MyGridView1.DataBind()

Flex - sorting a datagrid column by the row's label

I'm creating a table that displays information from a MySQL database, I'm using foreignkeys all over the place to cross-reference data.
Basically I have a datagrid with a column named 'system.' The system is an int that represents the id of an object in another table. I've used lableFunction to cross-reference the two and rename the column. But now sorting doesn't work, I understand that you have to create a custom sorting function. I have tried cross-referencing the two tables again, but that takes ~30sec to sort 1200 rows. Now I'm just clueless as to what I should try next.
Is there any way to access the columns field label inside the sort function?
public function order(a:Object,b:Object):int
{
var v1:String = a.sys;
var v2:String = b.sys;
if ( v1 < v2 ){
trace(-1);
return -1;
}else if ( v1 > v2 ){
trace(1);
return 1;
}else {
trace(0);
return 0;
}
}
One way to handle this is to go through the objects you received and add the label as a property on each of them based on the cross-referenced id. Then you can specify your label property to display in your data grid column instead of using a label function. That way you would get sorting as you'd expect rather than having to create your own sort function.
The way that DataGrids, and other list based classes work is by using itemRenderers. Renderers are only created for the data that is shown on screen. In most cases there is a lot more data in your dataProvider than what is seen on screen.
Trying to sort your data based on something displayed by the dataGrid will most likely not give you the results you want.
But, there is no reason you can't call the same label function on your data objects in the sortFunction.
One way is to use the itemToLabel function of the dataGrid:
var v1:String = dataGrid.itemToLabel(a);
var v2:String = dataGrid.itemToLabel(b);
A second way is to just call the labelFunction explicitly:
var v1:String = labelFunction(a);
var v2:String = = labelFunction(b);
In my experience I have found sorting to be extremely quick, however you're recordset is slightly larger than what I usually load in memory at a single time.

Resources