I'm making a simple web application in C#. The site which I'm working on contains a GridView. The GridView shows the data from an Oracle SQL data table.
Now I want to have filtering options for my table. But I want to make it very easy to switch through the columns and I want to make it possible to filter and maybe sort each column for itself.
I already made a TextBox which instantly filters a specific column. But I want to have the User to select the column. My idea is to have the user selecting a column and then he only has to write his criteria into the TextBox.
My main problem at the moment is to figure out how to make the user able to select a column and get its index/header-text.
For instance: I want to get the index/header-text if the user wants to filter/sort the column and somehow selected it.
I'm new on asp.net and I don't really know how to realize this. I've searched a lot to find any solutions, but there isn't much about this.
I used this for my filtering method (works):
dt = GridView1.DataSource as DataTable;
dt.DefaultView.RowFilter = string.Format("Name LIKE '%{0}%'", textBox1.Text);
GridView1.DataSource = dt;
GridView1.DataBind();
For sort by column specify inside the tag of the Gridview:
AllowSorting="true">
</asp:gridview>
For Getting the header text of any column:
Gridview.Columns[i].HeaderText
No possible to filter by columns with a checkbox on the header
Related
In my grid-view control 1000 records already there(binded from dataset) when I apply search criteria it shows 500 records.
Now I want to sort only that particular 500 rows when I click on the Grid column name.
MyGrid.Datasource = ds.Tables[0].DefaultView;
MyGrid.DataBind();
Note : Before applying filter(search), I do not want to sort my Grid.
Kindly help me to sort only the result gridview.
Create all the functionallity in your GridView that handles sorting by clicking on the column names OnSorting="MyGrid_Sorting" and AllowSorting="true".
If sorting works correctly set AllowSorting to false.
Now all you have to do is enable the sorting of the GridView in code behind. Preferably in the function that applies the search criteria.
private void applyFilter()
{
MyGrid.AllowSorting = true;
MyGrid.Datasource = ds.Tables[0].DefaultView;
MyGrid.DataBind();
}
I'm having the following problem:
What I have is a search function that runs a query on my database using a SqlDataAdapter.
I then use:
DataSet ds = new DataSet();
dataAdapter.Fill(ds, "table title");
gridView1.DataSource = ds;
gridView1.DataBind();
I originally got an error that I then had both a dataSource and dataSourceId, so I cleared the previously used ID. The code works, I can search the table and my query will return rows and update the table. The problem is that I want the data that it returns to be editable. I have my grid view editable, and before I run the query, I can edit the rows of the table. But after the search is run, and the gridView is filled with my DataSet, I'm unable to update the rows. If I click the edit button, it gives me an error saying the RowEditing event wasn't handled.
I looked into that event, and understand what's happening, if I were to program in the events for RowEditing/RowUpdating/RowUpdated etc. I could get it to work, but is there no better way to do this? Is there a simpler way to set my dataSource in C# and be able to maintain the editablity of the rows?
Thanks a lot!
I suppose part of the question would be would you want to? I guess you're looking for a SaveDataChanges() method which brings about a few design concerns. One is you have to remember that web pages are stateless so to an extent your web page post isn't aware of the data it's just pulled and bound to the UI. The grid view control in a round about way nicely gets the user to help you out here and click update on the row they've amended. With this you have your event handler and you can just focus on the data that's been changed and just send one UPDATE statement.
Does anyone know of a way to add columns to a WPF DataGrid on the fly? I'm looking to use a DataGrid in my GUI that will display standard data, and that will also allow the user to add and remove columns. When the user adds a column, the user should be able to specify a value that will be entered on each row of the column. I have used the DataGrid in the past by creating a class with properties and a list of these objects fills out the grid easily, where each object in the list is a row. If I could create a new class on the fly or add a new property on the fly to my existing class, it seems like this would be possible.
I've been looking for a way to add properties to an object on the fly as a possible solution, but I haven't found a way to do this yet, and I don't know if it's a feasible solution. I also haven't found a way to create a new class on the fly and again I don't know if it's possible. I have also tried using a DataTable, but I haven't been able to get that to work either.
Here are more details on what I want to accomplish. A screenshot is included below. The datagrid will be populated with data. The user can then enter a column header and column data. When the "Update Column" button is pressed,if the data field contains "!REMOVE_COL" then the column will be removed if it exists and will not be added if it does not exist. If the column doesn't exist, it will add the column and the value entered in the data field will be added to each row. If the column already exists, then the column data will be updated with the value in the data field.
Haha, nevermind on the screenshot. Apparently I don't have enough rep to post images. :/ You can find the screenshot here:
https://docs.google.com/document/d/1B4UVhVDqFdBbsUMOtMLK2PGTrnKLLux2leo_gas9rdw/edit?usp=sharing
Beware that accessing a WPF DataGrid at run-time it nothing like doing the equivalent for a WinForms DataGridView. Anyway, what you want to do is to create a method which builds and returns a DataSet (which may or may not have data, no data will merely define the column structure)
private DataSet BuildDataGridColumns()
{
// Build the data.
DataSet ds = new DataSet();
DataTable dt = new DataTable();
ds.Tables.Add(dt);
// Add the columns to the DataTable.
DataColumn indexCol = new DataColumn("Key Index", typeof(String));
dt.Columns.Add(indexCol);
DataColumn fileCol = new DataColumn("File Name", typeof(String));
dt.Columns.Add(fileCol);
DataColumn resCol = new DataColumn("Resource Name", typeof(String));
dt.Columns.Add(resCol);
return ds;
}
To create the DataGrid called dataGrid you then can use
dataGrid.ItemsSource = BuildDataGridColumns().Tables[0].AsDataView();
To remove/change the shown columns change the ItemSource, using the above should enable you to accomplish all that you require.
I hope this helps.
I have a blank/unbound GridView control on my form and I am binding it in the code behind like this:
GridView1.DataSource = _dataSet
DataBind()
Running this code populates my GridView control with all the columns and data that _dataSet has. I want to display only some of the columns, change the column names, and rearrange some of these columns too (i want the last column from the actual sql database table to be displayed first in my GridView).
Can someone show me how do do this?
Set the AutoGenerateColumns property of the GridView to false and manually create the columns. That would be the easiest way.
Another way is to also set AutoGenerateColums property to false but this time, append the colums to the columns property of the GridView. Use the .Clear and the Add methods provided
First of all, in your GridView markup, you can change the header text:
<asp:BoundField DataField="SOME_COLUMN" HeaderText="Comment" SortExpression="SOME_COLUMN" />
Be sure to set AutoGenerateColumns to false.
Second of all, you can filter using DataSource.FilterExpression = "col1 = 'this'", if your dataset is being populated by a SqlDataSource or similar.
I've written an article that focuses on different ways to create columns in a GridView. It also discusses how to change the name of a column etc. I hope it will be of use to those, who are new to the GridView: http://www.tomot.de/en-us/article/7/asp.net/gridview-overview-of-different-ways-to-bind-data-to-columns
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");