Adding column to a gridview - c#

I currently have a gridview that is set up and working properly but I ran into a little bit of an issue. My grid's datasource is being set from a DB table and one of the columns in the table is the numeric ID of another table. Rather than displaying this numeric ID in the grid, I need to display a description column off of the other table. Can I add an additional column to my current gridview just for display purposes?

Yes you can accomplish this by using a template on your ID field rather than creating a new field. This link has some examples. This way you can custom format your existing column.

You can use a data-set and bind this dataset to the gridview.
Using the data-set, you can add rows, columns. The below example is a good one to add rows/columns to the grid-view. You can also follow the same example with a little bit of tweaks.
http://www.codeproject.com/KB/aspnet/dynamic_Columns_in_Grid.aspx

Set AutoGenerateColumns to false, and define the columns that you want to display:
<asp:GridView ID="GridView1" runat="server" CellPadding="4" CellSpacing="0" AutoGenerateColumns="false" Width="100%">
<RowStyle BackColor="#ffffff" />
<AlternatingRowStyle BackColor="#f5f5dc" />
<HeaderStyle BackColor="Beige" ForeColor="#333333" Font-Bold="true" Height="25px" />
<Columns>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<ItemTemplate>
<%# Eval("Address") %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

The change should be made in 3 locations:
The datasource should have the desc column brought back with it. Do the join in the database and bring it all back at once so the front end will not need to make an insane amount of calls.
On display, you can set the id column to invisible. This can be done on the code behind or on the aspx.
On edit, if it is a value that is based off of a look-up table, you will need to create a drop down list. You can query for more info if edit is needed, if not, the top two points should cover your needs.

Related

DataBind and DataSource on a Gridview

I have done gridviews before, where I bind all SQL data from data safe reader or specify column names and binds at the javascript level. This is a bit different and I am a bit stumped on how to proceed.
I have a business class that handles all SQL data queries.
This class is called to Fetch a LIST. This list contains collections and child collections.
var _InfoList = BusinessObjectClass.Get(_CriteriaKey);
I can access the list as such:-
Txtbox1.Text = _InfoList.ID#.ToString();
Now I am trying to bind one of the collections in the LIST to a gridview.:-
C#:-
gvwMembers.DataSource = _InfoList.Members;
gvwMembers.DataBind();
Where Members is a collection...
But this syntax doesn't add any thing to the gridview...The gridview is empty.
Second methodology:-
I also tried doing something like this:-
List<BusinessObjectClass> Members = new List<BusinessObjectClass>();
Members = _InfoList.Members;
gvwVendors.DataSource = Members;
gvwVendors.DataBind();
But to no avail.. this is because the problem lies in the 2nd statement:-
Members = _InfoList.Members.... this is not a valid assignment...can anyone help with this?
After fleshing out some details in the comments, it is perfectly fine to have a simple GridView with no columns defined, but make sure AutoGenerateColumns is not false. The default is true. This will create a column for you, based on each property of the object being bound.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true">
</asp:GridView>
And in order to pick and choose which properties to display, define them in the <Columns> section.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Property1" HeaderText="Property1" />
<asp:TemplateField HeaderText="Property2">
<ItemTemplate>
<asp:Label ID="lblProperty2" runat="server" Text='<%# Eval("Property2") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

How to pull out headertext for gridview from database?

So here is the scenario. I need to pull out headertext from database for my gridview. I am going to give the column name of the given fields that I want from the database becuase I dont need to show all the columns from database to gridview. Here is what I have got so far. Here is aspx code.
<asp:GridView ID="gvCustProfile" runat="server" AllowPaging="True" DataKeyNames="custname" AutoGenerateColumns="False"
BorderStyle="None" GridLines="Horizontal" HeaderStyle-HorizontalAlign="Left"
ShowFooter="True" Width="125%" AllowSorting="True">
Here is what I got in CS file.
gvCustProfile.Columns["ACTIVE_YN"].HeaderText = GetColNameFromDictionary(inputColName, profileTable);
Is there any way where I can specify the column name I want from database and get the text value of it since I dont want to hardcode it at all. My code will pass the value like "ACTIVE_YN" and is going to match up in a dictionary (which is in database) and return me a user friendly description to the value I pass.

How can you read a gridview cell when editing besides textboxes?

I having a little dilemma and Im not sure how to work around this issue. If I am editing a gridview, I am reading the rows as such
cmd.Parameters.Add("#X", SqlDbType.Char).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[15].Controls[0]).Text;
cmd.Parameters.Add("#Y", SqlDbType.Char).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[15].Controls[0]).Text;
When the gridview is in edit mode, both cells X and Y have textboxes because that is the default edit mode. I created the columns manually and I would like for cell X to be "read only". How can I change the code so that I can read the cell X without a textbox? Help would be greatly appreciated. Thanks!
This might be a bit too simple, but how about:
var lblHolder = (Label) GridView1.FindControl("lblMyLabel")
cmd.Parameters.Add("#X", SqlDbType.Char).Value = lblHolder.Text
Just search for your label (or whatever is holding your value) directly
Then your Grid View can look something like this
<asp:GridView ID="gvData" runat="server" AutoGenerateColumns="false" Width="95%" Font-Names="Verdana">
<RowStyle Height="40px" />
<Columns>
<asp:TemplateField HeaderText="Col1">
<ItemTemplate>
<asp:Label ID="lblMyLabel" runat="server"
Text='<%# eval("MyDataBaseField")%>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Then you just need to connect a data source to the grid. There are of course loads of other ways to do it, but searching for controls by their Ids is a lot easier than trying to figure out their number in an array

How to create column that will autogenerate the serial number for Each row of a Gridview?

I have a Gridview with a list of Items. I want to show the Serial Number in One Column of a GridView like 1 2 3 4 5.....
You can do using Container.DataItemIndex in GridView. It will display serial numbers as above mentioned.
<asp:TemplateField HeaderText="SNo.">
<itemtemplate>
<%#Container.DataItemIndex + 1 %>
</itemtemplate>
</asp:TemplateField>
I would suggest adding this data to the source you are using to bind to the GridView. So If you have a ICollection<Product> that you are binding to the GridView, add the Serial number to the product class.
Alternatively, you could turn the ICollection<Product> into a Dictionary<int, Product> and bind the key to the Serial number column.
It's difficult to know what would suit without knowing how you are biding your data.
Add this Template field inside the tag Columns. You will get AutoGenerate SNo.
For More Information see the below link
How to create autogenerate serial number column in GridView
<asp:GridView ID="GridEmployee" runat="server" AutoGenerateColumns="false"
CellPadding="5" Style="background-color: rgb(241, 241, 241); width: 50%;">
<Columns>
<asp:TemplateField HeaderText="S.No">
<ItemTemplate>
<%#Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I hope this helps you. Thanks
Here is simple way...
<ItemTemplate>
<%#Container.ItemIndex + 1 %>
</ItemTemplate>
Googling for serial number column gridview will give you millions of results that discuss the same thing. Read the articles/tutorials/discussion and implement it.
Please do Google before you ask.

Set DetailsView as selected row of GridView

I am creating a GridView/DetailsView page. I have a grid that displays a bunch of rows, when a row is selected it uses a DetailsView to allow for Insert/Update.
My question is what is the best way to link these? I do not want to reach out to the web service again, all the data i need is in the selected grid view row. I basically have 2 separate data sources that share the same "DataObjectTypeName", the first data source retrieves the data, and the other to do the CRUD.
What is the best way to transfer the Selected Grid View row to the Details View? Am I going to have to manualy handle the Insert/Update events and call the data source myself?
Is there no way to link these two so they use the same data source ?
<asp:GridView ID="gvDetails" runat="server" DataKeyNames="ID, Code"
DataSourceID="odsSearchData" >
<Columns>
<asp:BoundField DataField="RowA" HeaderText="A" SortExpression="RowA" />
<asp:BoundField DataField="RowB" HeaderText="B" SortExpression="RowB" />
<asp:BoundField DataField="RowC" HeaderText="C" SortExpression="RowC" />
....Code...
<asp:DetailsView ID="dvDetails" runat="server" DataKeyNames="ID, Code"
DataSourceID="odsCRUD" GridLines="None" DefaultMode="Edit" AutoGenerateRows="false"
Visible="false" Width="100%">
<Fields>
<asp:BoundField DataField="RowA" HeaderText="A" SortExpression="RowA" />
<asp:BoundField DataField="RowB" HeaderText="B" SortExpression="RowB" />
<asp:BoundField DataField="RowC" HeaderText="C" SortExpression="RowC" />
...
The standard way to do it would be to have the selected item of the griview be a control parameter to the objectdatasource you have wired up to the detailsview. I would probably not worry too much about the overhead of retreiving data that you already have unless you are catering to users with such slow connections that you want to avoid roundtrips to the webserver at all costs.
If you really want to avoid that thenyou could pull the data out of the gridview using javascript/jquery and then do your inserts/updates via ajax calls. It would require lots more coding though.
This is a really old thread, but in case anyone came here looking for an answer like I did, a simple solution is to add this function to your code:
(Note that this only works if the rows in your GridView match the entries in your DetailsView.)
protected void GridView1_OnSelectedIndexChanged(object sender, EventArgs e)
{
DetailsView1.SetPageIndex(GridView1.SelectedIndex);
}
And modify the GridView and DetailsView to include these settings:
<asp:GridView ... OnSelectedIndexChanged="GridView1_OnSelectedIndexChanged" ... >
<asp:DetailsView ... AllowPaging="True" ... >
This will make the selected page in the DetailsView match the selected index in the GridView.
You can hide the paging options in the DetailsView properties if you dont want users to navigate using paging in the DetailsView.

Categories

Resources