I have a dynamically created gridview in c# asp.net that I need sorted when you load the page. How can this be done? I am using gridview1.DataBind() to bind.
try this
DataTable table = GetTable();
table.DefaultView.Sort = "SortCondition";
//
// Display all records in the view.
//
DataView view = table.DefaultView;
Now Bind the grid
GridView1.DataSource=view;
GirdView1.DataBind();
Related
objRetailPL.branch = Request.QueryString["branch"].ToString();
objRetailPL.fromdate = Convert.ToDateTime(Request.QueryString["fromdate"].ToString());
objRetailPL.todate = Convert.ToDateTime(Request.QueryString["todate"].ToString());
DataTable dtget = new DataTable();
dtget = objRetailBAL.getWhReport(objRetailPL);
GVWHReport.DataSource = dtget;
I want to display datatable values in grid with out using bound fields and template fields. Directly from database to datatable and in grid. Because I am using pivot table.
GridView has a AutoGenerateColumns property. Make sure it's set to true. This makes sure that bound fields are automatically created for each field in the data source.
Set the DataTable as the gridview's DataSource and call DataBind() method on GridView, something like:
MyGridView.DataSource = dtget;
MyGridView.DataBind();
aspx page
<asp:GridView ID="gvTest" runat="server" AutoGenerateColumns="true"></asp:GridView>
C# Code:
gvTest.DataSource = dtget;
gvTest.DataBind();
You need to set your DataTable as DataSource and then invoke DataBind method on your GridView. Someting like this
GridView1.DataSource = dtget;
GridView1.DataBind();
I have a FormView with paging bound to a SqlDataSource. I am trying to find out how to access the datasource currently bound to the FormView. For example, if I am on page 2, how do I access the second row of the datasoure? I only need to read certain values for comparison; the data will not be changed. Is it possible to get this as a DataRow or DataTable object?
Use this code to access the DataTable in your SQLDataSource
DataSourceSelectArguments args = new DataSourceSelectArguments();
SqlDataSource mds = (SqlDataSource)MyFormView.DataSourceObject;
DataView view = (DataView)mds.Select(args);
DataTable dt = view.ToTable();
DataRow dr2 = dt.Rows[1];
I tested this with a ListView, not a FormView, hope it works for you as well
I'm beginner in .Net ,so maybe my question will seem naive to some of you.
I have DataGridView table in WinForm project:
It contain three columns(image,combobox and textBox columns).
Any idea how to create and attach rows to this table?
Thank you in advance!
You create a data source, then bind the data source to the grid's DataSource property. You then add a record to your data source.
// create data source
BindingList<Shape> dataSource = new BindingList<Shape>();
// add record to data source
dataSource.Add(new Shape("Some Contour", "Circle", "Some Name"));
// bind data source
yourDataGridView.DataSource = typeof(BindingList<Shape>);
yourDataGridView.DataSource = dataSource;
Set the DataPropertyName of each column to matches the names of the fields in your Shape class.
DataGridViewTextBoxColumn colName = new DataGridViewTextBoxColumn();
colName.DataPropertyName = "Name";
yourDataGridView.Columns.Add(colName );
However, I recommend you use Virtual Mode instead to keep your data separate and decoupled.
If you wish to accept inputs from user, you have to create a form on this page using which the user can provide inputs. Take those values and add them to a DataTable. Following is a sample snippet showing it:
DataTable dt = new DataTable();
dt.Columns.Add("Contour",typeof(string)); //I am assuming that you will store path
//of image in the DataTable
dt.Columns.Add("Shape",typeof(string));
dt.Columns.Add("Name",typeof(string));
Keep adding new rows to the DataTable as you receive inputs from the user:
DataRow row = dt.NewRow();
row["Contour"] = txtContourPath.Text;
row["Shape"] = ddlShape.SelectedValue;
row["Name"] = txtName.Text;
dt.Rows.Add(row);
Assign above DataTable to DataSource property of the GridView.
dgv.DataSource = dt;
You can use method:
dataGridView1.Rows.Insert(...)
dataGridView1.Rows.Add(...)
Jay's answer : use dataGridView1.DataSource = dataSource;
Hope I can help you.
I am trying to add an initial unbound entry into a bound ComboBox. I am trying an approach very similar to the answer to the following post:
How to insert 'Empty' field in ComboBox bound to DataTable
However, the main difference is that I have strict requirements and cannot add a row to the actual datatable (as this is used by other components). So, my solution is to add the additional row to the dataview instead of the datatable:
public void FillVendorComboBox(DataSet1.VendorDataTable vendors)
{
//Create a custom view of the vendor table
DataView view = new DataView(vendors);
//Add a new row to the view with default values
DataSet1.VendorRow vendorRow = (DataSet1.VendorRow)view.AddNew().Row;
vendorRow.Name = "a";
//Sort the view according to the vendor name
view.Sort = vendors.NameColumn.ColumnName;
//Bind the view to the combo box
cbxVendor.DataSource = view;
cbxVendor.DisplayMember = vendors.NameColumn.ColumnName;
cbxVendor.ValueMember = vendors.IdColumn.ColumnName;
}
The problem is that the sorting is not working as expected. The added value is always sorted to the end of the ComboBox:
BC (bound)
Shell (bound)
a (added) <-- why is this sorting to the bottom?
Also please note that the VendorRow.Name is datatype System.String and VendorRow.Id is datatype System.Int32.
How to sort datatable obtained by GridView according to some column.
I am trying to do something like this but it is not working.
protected void GridView_Sorting(object sender, GridViewSortEventArgs e)
{
if (sender.GetType() != typeof(GridView))
return;
DataTable dt = (DataTable)((GridView)sender).DataSource;
DataTable dtClone = dt.Clone();
dt.AsEnumerable().OrderBy(row => row.Field <string>(e.SortExpression));
((GridView)sender).Source(dtClone);
}
You can either re-bind the gridview to the sorted datatable, or you can apply the sort expression to the gridview itself rather than the underlying bound table.
This is a pretty good summary with links to examples:
http://msdn.microsoft.com/en-us/library/hwf94875.aspx
Add the attributes OnSorting="gridView_Sorting" AllowSorting="true" in Gridview
and SortExpression="ColumnName" in the column
and implement OnSorting Event.
check the links
Sorting the GridView's Data
How to sort the data in grid view?
How to sort data into GridView
Sorting Data in a GridView
How to sort GridView?
I've done something like this in the past to sort a DataTable:
DataTable dt = new DataTable(); // your data table
dt.DefaultView.Sort = "your_field" + "sort_direction";
DataView dv = new DataView(dt);
dv.Sort = ("your_field" + "sort_direction");
yourGridView.DataSource = dv;
When sorting, make sure to cast your table to a DataView. You'll save yourself a lot of time over manually implementing sorting methods.