GridView. Take attributes name - c#

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.

Related

How to bind datatable with grid view without using bound and template fields in asp.net?

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();

Getting data from a FormView's currently bound row

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

How to create and attach rows to this DataGridView?

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.

Bind a DataGridView to a DataSet/DataTable

I have two DataTables which I add to a DataSet so that I can achieve a master-view layout. However, I wish to change the appearance of the table, so I need a DataGridView. The problem is that the data below is not binding to the DataGridView and I am getting a null pointer exception when I try to color one of the columns. The column does exist in the data source.
I have tried AugoGenerateColumns = true, BindingSource b = new BindingSource() and this still wouldnt work.
Does anyone know how?
DataSet ds = new DataSet();
ds.Tables.AddRange(new DataTable[] { dtm, dtd });
ds.Relations.Add("Relationship_name", dcmpk, dcdfk);
DataGridView dgv1 = new DataGridView();
dgv1.DataSource = ds.Tables[0];
//Null pointer exception
dgv1.Columns[0].DefaultCellStyle.BackColor = Color.Red;
I didn't add my columns to a DataGridView because the DataTable had a PrimaryKey property which was useful for my table layout.

Adding a Combobox in DataGridView through Datatable

I have a DataGridView and I am populating it through a DataTable. On initializing, I add DataColumns to the DataTable and then set the DataSource of the DataGridView to the DataTable. Here is the code:
DataTable mTable = new DataTable("Test");
DataColumn col = new DataColumn;
col.DataType = System.Type.GetType("System.Boolean");
col.ColumnName = "First";
col.ReadOnly = false;
mTable.Columns.Add(col);
col = new DataColumn;
col.DataType = System.Type.GetType("System.String");
col.ColumnName = "Second";
col.ReadOnly = false;
mTable.Columns.Add(col);
this.myGridView.DataSource = mTable;
This works fine however, I want to make one of the columns showup as a combobox. I think I have to do something with the Column's datatype but I am not sure how to do this. Can anyone give me any pointer towards this?
This is old, but I thought I'd try to answer it anyways since I had the same question myself a while ago.
First of all, you cannot make a DataTable column of type DataGridViewComboBoxColumn. DataColumn inherits from System.Data, while DataGridView*Columns inherit from System.Windows.Forms.
What you can try is this: Once your DataTable is bound to your DataGridView, the columns from the DataTable should show up on the DataGridView in the designer. If you then expand the Columns property of your DataGridView, you should see your columns from your DataTable have been added to the list. Select the column that you want, and in the right-hand pane there will be a property called 'ColumnType'. The default type is DataGridViewTextBoxColumn, but you can change it to DataGridViewComboBoxColumn.
change the declaration to this:
DataGridViewComboBoxColumn column1 = new DataGridViewComboBoxColumn()

Categories