Setting a dictionary as datasource - asp.net

I have the following dictionary and like to set this as the datasource to my Gridview in my asp.net page
Dim myList As New Dictionary(Of Person, string)
the dictionary key is an object of Person which has Id,FirstName and LastName.
and I like to bind the above dictionary to a gridview which shows First name, Last name and Address
<asp:TemplateField HeaderText="First Name" SortExpression="FirstName">
<EditItemTemplate>
<asp:TextBox ID="txtFName" runat="server" Text='<%# Bind("FirstName")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
the next column will be last name and the third will be the address - which is the value property of dictionary
is it possible to bind the above dictionary to a gridview at all?
Thank you.

Your Dictionary is made up of KeyValuePair elements, which exposes two properties: Key and Value.
Since you want to bind items that are accessible through the Key property of each Dictionary entry, you should reference these properties in your databinding as Key.FirstName, Key.LastName, Key.Address, etc.
So given your example above, binding the TextBox should look like:
<asp:TextBox ID="txtFName" runat="server" Text='<%# Bind("Key.FirstName")%>'></asp:TextBox>

Related

Getting the bound type of templated column controls in gridView

Hi I have following grid view
<asp:GridView ID="grdSettings" runat="server" Height="400px" Width="100%"
AutoGenerateColumns="false" >
<Columns>
<asp:TemplateField HeaderText="XYZ" BoundFieldName="XYZTypeName">
<ItemTemplate>
<asp:Label ID="lblCustomerName" runat="server" Text='<%# Bind("CustomerName") %>'>
</asp:Label>
<asp:Label ID="lblCostumerId" runat="server" Text='<%#Bind("CustomerId") %>'></asp:Label>
</asp:TemplateField>
</Columns>
</asp:GridView>
Which is bound by List of Customer ,class is as follows
Class Customer
{
public string CustomerName { get; set; }
public int CustomerId { get; set; }
}
Now on a method named GetGridStuff() , i need to iterate in every column and get the type that was bound to controls in template field. For example in case of the first control in the template field
<asp:Label ID="lblCustomerName" runat="server" Text='<%# Bind("CustomerName") %>' >
</asp:Label>
I need to know what type of property data it contains , in this case its CustomerName. I need to get this dynamically at run time as to write code in part of program which is not aware if this grid structure. I have the grid object with me and i can access all properties.
Let me try this, being away from coding for more than 5 years now and if I understand the problem correctly as you may want to handle it through server side code.
Look for an event called ItemBound (or similar forgive me for my memory), this gives you values of all the items in current row (Properties). You also need to declare temp control types (label,textbox etc) and assign corresponding value to these controls using e.FindControl with appropriate type casting, e.g. Label l = (Label)e.Findcontrol("Name")
downside of the approach you should avoid too much of process as it is going to execute on every row creating of the grid.
Let me know if you still want a precise code to handle the problem but otherwise this description should at least help you to look for Row Level Event to crack the prob and also encourage you to stick to tech forums :)

get the selected elements from a ListBox(with multipleselect) which is inside a FormView Template

So I have a FormView. Inside the InsertTemplate I have a ListBox
<asp:ListBox ID="AppsList" runat="server" DataSourceID="SqlDataSource3"
DataTextField="Name" DataValueField="Id" SelectedValue='<%# Bind("Id") %>'
AppendDataBoundItems="True" SelectionMode="Multiple"/>`
In my ObjectDataSource I only get the first selection (If I have two or three selected elements, only the id of the first one is submitted).
public void InsertOperator(string companyName, string address, string phoneNumber, string email,
string NrPoints, string perimeterPointsList,object Id)
I actualy put the "object" type for parameter Id (coming from the ListBox) to see what type is passed to the method. It is a string. So how can I make it be the list/array of all selected Ids, not just the first selected?

Deleting Record Using ASP.NET Repeater and Entity Framework

I have an entity called PendingPartners which has a navigation property to the Residents entity. I'm returning information from these objects like so:
Dim getSent = (From p In dbContext.PendingPartners _
Join r In dbContext.Residents _
On p.people_id_des Equals r.people_code_id _
Where p.people_id_ini = people_id _
Where p.semester = semester _
Where p.year = year _
Select r.person_name).Distinct
rptrSent.DataSource = getSent
rptrSent.DataBind()
As you can see above, the data is then bound to a repeater control the code for which looks like this:
<asp:Repeater ID="rptrSent" runat="server">
<ItemTemplate>
<asp:Button ID="btnDeletePartner" runat="server"
Text="<%# Container.DataItem %>" />
<br />
</ItemTemplate>
</asp:Repeater>
Now, I want to make it so that when someone clicks the Delete button it actually deletes that record from the entity - just the PendingPartner portion, no need to rollup to the Resident object.
Now, to do this I think I need to:
Add the ID column to the EntitySQL query, e.g. Select p.id, r.person_name.
Bind the ID column to the repeater as say CommandName or CommandArgument.
Change the way the binding is for the text - since the repeater now has two columns.
I know how to update the EntitySQL, but I always get confused with then getting this info. into the repeater...help?
I think your need to use the bind function in the repeater
The Button's Text uses "person_name" and the command argument uses the "id" and the command name would be "delete".
like so:
<asp:Repeater ID="rptrSent" runat="server">
<ItemTemplate>
<asp:Button ID="btnDeletePartner" runat="server"
Text='<%# bind("person_name") %>' CommandArgument='<%# bind("id") %>' CommandName="Delete" />
<br />
</ItemTemplate>
</asp:Repeater>

How to bind one column to gridview which is not present in database

How to bind one column to the gridview which is not present in the database?
I want to display the total unit in the last column named Total Unit but it is not present in database.
I got an argument exception:
Column 'tunit' does not belong to
table.
foreach(DataRow row in dt.Rows )
{
object[] obj=new object[2];
obj[0] = row["Transaction_Id"];
obj[1] = row["tunit"];
dtgrid.Rows.Add(obj);
}
The best solution to this problem is to implement unbound column as it is explained in the Unbound Columns topic.
if you are binding a list, you can add a property to that list and bind to the grid using eval("PropertyName")
When you prepare the select query make sure you have one more extra column with the total value you want for that particular row, then you can specify a column in the gridview as follows, make sure you specify
'AutoGenerateColumns = false;
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="sds_datasourceName">
<Columns>
<asp:TemplateField HeaderText="Total" >
<itemtemplate>
<asp:Label ID="Total" runat="server" Text='<%# Bind("Total") %>'></asp:Label>
</itemtemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

How to do a data lookup field in a DetailsView?

I have a master-detail page for Customers. Select a customer from the list and a details view opens with Name, Address...etc.
I've been asked to add a field listing the Sales Rep servicing that customer.
I want the new field to hold the foreign key to the SalesRep table: the SalesRepID. An integer.
I am not sure how to "wire up" the ItemTemplate field for displaying the Sales Rep name in a label. Also not sure about the EditItemTemplate dropdown list of possible Sales Reps.
I know I need to create a datasource to retreive the Sales Reps into a dataSet.
Let's call it "SQL_Reps_source".
A DropDownList seems to only like a list of values, and doesn't seem to handle Keys and Values like a HashTable or SortedList would.
Any advice on how to go about this?
Thanks
The following example adds a DropDownList to the EditItemTemplate of the DetailsView. The DataSourceID is set to the data source control that retrieves the sales reps names and id's.
<asp:TemplateField HeaderText="Sales Rep">
<EditItemTemplate>
<asp:SqlDataSource ID="SqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
SelectCommand="SELECT SalesRepID, SalesRepName FROM SalesReps">
</asp:SqlDataSource>
<asp:DropDownList ID="DropDownList" Runat="server"
DataTextField="SalesRepName" DataValueField="SalesRepID"
SelectedValue='<%# Bind("SalesRepID") %>'
DataSourceID="SqlDataSource">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label" Runat="server"
Text='<%# Bind("SalesRepName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
For display purposes - return the NAME of the Sales Rep. rather than ID
DropDownList (ddl) handles both Values and Keys just fine, the naming is a bit off though. ddl.DataTextField property will hold the Name, ddl.DataValueField property will hold the ID. (see this answer for more details on this)
In the EditTemplate you'd need to fill the DropDown as described above, and then set its SelectedValue to the ID of Sales Rep. in quesion.
Hope this helps.
When you bind the dropdownlist do this:
ddl.DataSource = SQL_Reps_source;
ddl..DataTextField = "fullname";
ddl..DataValueField = "id";

Resources