How change column names randomly in gridview? - c#

I am struggling to show different views of gridview with database values. Here is my requirement.
Database Table:
I need to show one drop downlist with three values 1,2,3. If user selects 3 i Need to show a grid like below
Age,AnnualSales and Assortment are names which are coming dynamically from database.
High,Medium and Core all are values
Here my question is I need to show same attribute names.
Like If the name changes from "Assortment" to "Location" I need to show it in different page index .
In page index 2 i need to show like below.
Please help with solution

One solution could be, generating columns in codebehind. e.g.
BoundField bf = new BoundField();
bf.HeaderText = "HeaderText";
bf.SortExpression = "Datafield";
bf.DataField = "Datafield";
GridView1.Columns.Add(bf);
One could write own TemplateClasses if required. There are lot of examples over net.
one would be
http://www.codeproject.com/KB/aspnet/create_template_columns.aspx
Tweak column headers or columns of gridview as required.
hope this helps you.

You can do this very well by setting the property dynamically.
GridView1.Columns[2].HeaderText = "Location";
I have worked pretty well using GridViews and databound controls in most dynamic ways.

Related

Show a particular column data in form of buttons or links

I have a query. I want to show a particular column data from a table in form of links. Suppose 5 rows and there in that column, I want to show 5 buttons or links.
Now I know how to bind in gridview or dropdown. But I'm totally unsure about this.
Any ideas will greatly help me.
Hi basically you have two options that you can implement in many ways.
Set the data in the view model in your case create a property that display a collection of items based on the right column
In the view (ex: wpf) create a data template that display the row according the column

RDLC Report not paging with dynamically bound datasource

I have a rdlc report I want to bind a list of my own custom class to as a data source.
I've used some code recommended on this site to build the DataTable
This works really well
//convert my list of Invoices to a DataTable
var dt = invoices.CopyToDataTable(dataTable, LoadOption.PreserveChanges);
//Setup a new DataSource
var rds = new ReportDataSource {Name = "ReportDataSet", Value = dt};
//Add that datasource to my ReportViewer
rvSampleInvoice.LocalReport.DataSources.Add(rds);
//Map my report path, etc
rvSampleInvoice.LocalReport.ReportPath = Server.MapPath("etc");
This all works fine, when I load my page, the report shows, but it only has one 2 pages. the first is the first entry in my invoices collection, and when I click next page, it's a blank page.
I've added a gridview to the page, and bound the DataTable (dt) to it and it displays as a nice table with a row of headers and 20 rows of data.
I'm wondering if I've got something wrong on how to bind a RDLC to my List of Invoices
This SO answer has has solved the issue. There's gotta be a better way of doing this, so if anyone knows a better way to have multiple pages for one result set, let me know
Update:
Ok, I'll explain what I've learnt now more about RDLCs for future archeologists. You can't just drag and drop data into a page, bind a list of items and expect it to duplicate the sample page you've made however many times there are items in your list.
You need to drag a table into the report and tick the "page break" option in the SO solution I linked to. Then stretch the table across the page, add a bunch of rows and columns. The fill in the cells you want in your page.
It takes a bunch of cell merging and stretching to get it to look the way I want, so if there's a better way, someone please make a post.

Finding the field type of GridView column

I'm working on an ASP.NET page that mutates to the MDB file fed to it. It currently allows you to choose a table to work with, and from there what row to edit, and fills the page with text boxes and fills them with data. This all works but it poses a problem when it isn't a string based field type, for example, what I have a problem with is determining when the cell being targeted is a CheckBoxField type.
My question would be, how can you get the column web control value from a filled GridView?
I think you want your grid to behave somthing like example explained:
http://www.codeproject.com/Articles/37207/Editable-Gridview-with-Textbox-CheckBox-Radio-Butt
Download source code for your refrence.
Hope this will help.
Do you have access to the table? You can try getting the type fron the column collection (table.Columns[name].DataType (from the top of my head)
You can then use the column type to select a control.

Can detail row use the same headers of master row on master-detail grid?

I have a DataTable holding many data which some of them is relative.
My objective is group relative records together and only show one of them until the user manually expand it to see all other records on that group.
First, I used group mechanism and build the text from represent record. But this way, I must handle text space to match with column header size, which column is hide or visible, etc.
So, I used master-detail (using event), it looks better. But when I expand the record, other records appear on their own headers, not the headers of master view.
My question is how to make detail rows showing in the same header of master row because they have the same member. It's no need to create another view for them! Is there any option or something else to do that?
P/S: My trial period is expired, so I cannot ask DevExpress team for support!
Update: This is what I had
But here is what I want
That means, the detail rows will not have any header line, and use the same layout as master row. Resize columns on main view and it affects on detail row too.
Thanks
Do This !
Use the ViewRegistered event to get the view and set look and feel.
the e.View is the new registered view..
GridControl_ViewRegistered(object sender,ViewOperationEventArgs e)
{
GridView view = e.View;
// do look and feel here
}
And just buy the devExpress license.
What Willie means is that when you subscribe to the `viewRegistered event you can access the new view. this is the only way, as far as i know, to access the opened detail view. At this point you can set the column headers etc. to have to same look and feel as your master view.
like:
GridControl_ViewRegistered(object sender,ViewOperationEventArgs e)
{
GridView view = e.View;
foreach Column column in view.Columns
{
// set the column look and feel to whatever you want it to be here
}
}

Add user generated content to a database in asp.net

In page_load, I create a table and fill it with data gathered from a database. Then after allowing the user to modify it, I need to make the changes to the database. But I'm not sure how to do this. I have been looking around and now am more confused then when I started. My code to create the table looks like this:
delList.Controls.Clear();
Table tbl = new Table();
tbl.ID = "tbl1";
tbl.BorderWidth = 1;
delList.Controls.Add(tbl);
This code gets added to a placeholder:
<asp:PlaceHolder ID="delList" runat="server"></asp:PlaceHolder>
I want to have a button at the bottom of the page to allow the user to select in order to save the changes but I can't figure out how to do this. Mostly because there will be a lot of rows that need to be updated. I was thinking it might be easiest to parse through the table using javascript and then somehow calling a function to make update the database one row at a time. But I'm not sure if this is even possible.
All help will be greatly appreciated.
It would be a lot easier if you used a control that is specific to this kind of operation. I mean a control like GridView.
GridView displays the values of a data
source in a table where each column
represents a field and each row
represents a record. The GridView
control enables you to select, sort,
and edit these items.
The GridView control is used to
display the values of a data source in
a table. Each column represents a
field, while each row represents a
record. The GridView control supports
the following features:
Binding to data source controls, such as SqlDataSource.
Built-in sort capabilities.
Built-in update and delete capabilities.
Built-in paging capabilities.
Built-in row selection capabilities.
Programmatic access to the GridView object model to dynamically set properties, handle events, and so on.
Multiple key fields.
Multiple data fields for the hyperlink columns.
Customizable appearance through themes and styles.
i dont get how the User Can modify content in a Table, unless you have some form Controls like Textbox, Dropdown list?
You should be creating form fields dynamically, and adding it to a container/form on the Page along with a button (which could do a postback/ or handle JavaScript).
You Could iterate through all controls in a parent container or add custom attributes to your form elements, do that you can identify those fields in JS/Clientside.

Categories