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
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'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.
Where I can get column names if i flled gridview by DataSource dynamically?
OracleDataAdapter adapter = new OracleDataAdapter();
DataTable tableD = new DataTable();
tableD.Locale = System.Globalization.CultureInfo.InvariantCulture;
adapter.SelectCommand = oracleCom;
adapter.Fill(tableD);
tableResults.DataSource = tableD.AsDataView();
tableResults.DataBind();
Where tableResults is GridView.
This code doesn't work:
updatingAtributes += tableResults.Columns[i].HeaderText;
Columns property of GridView will be set if you configure the columns on GridView. If you are relying on default behavior of GridView to render columns by setting AutoGenerateColumns property to true, then the Columns collection is not set with any value. In such case, you will have to use the object that you used as DataSource of the GridView (which is the DataTable tableD in your case) to get the column name.
i have a DataGridView on the Form. I select some data from database and load them to datatable and after that i make reference this datatable to grid's datasorurce as below.
string sql = "";
sql = "SELECT id,name,surname,code FROM t_persons";
DataTable dt = new DataTable();
...
adapter.Fill(dt);
grid.DataSource = dt;
and after that i want to add new row to this grid with grid.Rows.Add() method. But every time it gives an error Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.
So whatis the problem and how can i solve it.
You should add row to the DataTable, not to the DataGridView. That is what the exception is saying. Try:
DataRow newRow = dt.NewRow();
dt.Rows.Add(newRow);
Please you can add row directly to datatable and it's effect on gridview because it's bind to datatable.
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();