Gridview Dropdownlist binding - asp.net

I am having a lot of trouble getting a dropdown to bind with data from my database with the appropriate departments.
This is what I have so far:
HTML:
<asp:GridView ID="gridDepartmentHistory" runat="server" AutoGenerateColumns="False" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True">
<Columns>
<asp:TemplateField HeaderText="Department">
<ItemTemplate>
<asp:Label ID="lblDepartment" runat="server" Visible="true" Text='<%# Eval("Department")%>'></asp:Label>
<asp:DropDownList ID="ddlDepartment" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Start Date" HeaderText="Start Date" />
<asp:BoundField DataField="End Date" HeaderText="End Date" />
</Columns>
</asp:GridView>
Code behind:
Protected Sub gridDepartmentHistory_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gridDepartmentHistory.RowDataBound
If (e.Row.RowType = DataControlRowType.DataRow) Then
Dim ddlDepartment As DropDownList = CType(e.Row.FindControl("ddlDepartment"), DropDownList)
Dim list As ICollection(Of Department) = Department.hrGetDepartmentList() 'Class method to fill a collection of items with the Department's Name and ID
ddlDepartment.DataSource = list
ddlDepartment.DataTextField = "Name"
ddlDepartment.DataValueField = "ID"
ddlDepartment.DataBind()
Dim dept As String = CType(e.Row.FindControl("lblDepartment"), Label).Text
ddlDepartment.Items.FindByText(dept).Selected = True
End If
End Sub
When I run this it throws an exception saying:
Object reference not set to an instance of an object.
BTW: I am using this tutorial to help me through: http://www.aspsnippets.com/Articles/How-to-populate-DropDownList-in-GridView-in-ASPNet.aspx
Any help would be greatly appreciated! Thank you!

You simply need to retrieve dept id and store it in gridview as hidden (if you don't want to display it).
Dim dept As String = CType(e.Row.FindControl("lblDepartmentId"), Label).Text
ddlDepartment.SelectedValue = dept;
Hope it helps.

Your problem is that you ara trying to find the contorl in the row but is inside a table cell.
Try this.
Dim cbo As DropDownList = CType(YourDGV.Rows(x).Cells(0).FindControl("ddlDepartment"), DropDownList)

Related

Gridview Edit Mode - Drop down list does not show blank value

I have a drop down list 'Country', 'city' text box and an 'Add' button. The country drop down is NOT mandatory so I can just add a city without a country, adding an empty 'country' to the gridview works OK. the problem when I click on 'Edit' in the gridview it binds it to the first country in the list it does not just show a blank:
<asp:DropDownList ID="DDLCountry" runat="server" AutoPostBack="true" AppendDataBoundItems="true" OnSelectedIndexChanged="DDLCountry_SelectedIndexChanged" InitialValue="">
<asp:ListItem Text="------------------------ Select ------------------------" Value="" />
</asp:DropDownList>
<asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
<asp:Button ID="btnNewLList" runat="server" OnClick="btnNewLList_Click" Text="Add new Country"/>
<asp:GridView ID="gvAddNewCountry" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" OnRowCommand="gvAddNewCountry_RowCommand" OnRowDeleting="gvAddNewCountry_RowDeleting" OnRowDataBound="gvAddNewCountry_RowDataBound" OnRowUpdating="gvAddNewCountry_RowUpdating" OnRowEditing="gvAddNewCountry_RowEditing" OnRowCancelingEdit="gvAddNewCountry_RowCancelingEdit" ShowHeaderWhenEmpty="True">
<EmptyDataTemplate>
No Data
</EmptyDataTemplate>
<Columns>
<asp:TemplateField HeaderText="Actions">
<ItemTemplate>
<asp:Button ID="btnEdit" runat="server" Text="Edit"/>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType="Button" ShowEditButton="true" ShowCancelButton="true">
</asp:CommandField>
<asp:TemplateField HeaderText="Country>
<ItemTemplate>
<asp:Label ID="lblCountry" runat="server" Text='<% #Eval("Country") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DDCountry" runat="server" AppendDataBoundItems="True" AutoPostBack="false"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
Code behind:
Protected Sub gvAddNewCountry_RowCommand(sender As Object, e As GridViewCommandEventArgs)
If e.CommandName = "Edit" Then
Dim rowCountryToEdit As Integer = e.CommandArgument
Dim ddListCountry As DropDownList = (CType(gvAddNewCountry.Rows(CInt(e.CommandArgument)).FindControl("DDCountry"), DropDownList))
ddListCountry.DataSource = (From x In Country Where x.Domain = "lCountry" Order By x.Description Select x).ToList()
ddListCountry.DataTextField = "Description"
ddListCountry.DataValueField = "ID"
ddListCountry.DataBind()
End If
End Sub
Thanks for your help X
Ok, so when you have/want a ddl in a gv row?
We require TWO steps.
First step: Load up the list of choices for the dll
2nd step: set the ddl to the current row value, or blank (no choice) if null no value exists yet for the current row. This ALSO means we have to get/grab the current row value for the dll, and set the ddl to reflect this existing choice.
So, this is a two step process.
And the "event" we typical use for this is the row bind event. (all such controls from listview, gridview and more have this event).
Also, a VERY nice helper tip? During (but ONLY during) the data bind event, you have FULL USE of ALL columns from the data source - EVEN COLUMNS NOT in the gv!!!
I don't have a entity database first setup that you have, but lets load up a gv with a combo box:
So, our gv:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CssClass="table table-hover" Width="50%"
DataKeyNames="ID">
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="First Name" />
<asp:BoundField DataField="LastName" HeaderText="First Name" />
<asp:BoundField DataField="HotelName" HeaderText="Hotel Name" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Description" HeaderText="Descripiton" />
<asp:TemplateField HeaderText="Rating">
<ItemTemplate>
<asp:DropDownList ID="cboRating" runat="server"
DataTextField="Rating"
DataValueField="ID">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And our code to fill is this:
Dim rstRating As New DataTable
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
LoadGrid()
End If
End Sub
Sub LoadGrid()
' get data for rating combo
rstRating = MyRst("SELECT ID, Rating FROM tblRating ORDER BY ID")
' get data for grid
GridView1.DataSource = MyRst("SELECT * FROM tblHotelsA ORDER BY HotelName")
GridView1.DataBind()
End Sub
Public Function MyRst(strSQL As String) As DataTable
Dim rstData As New DataTable
Using conn As New SqlConnection(My.Settings.TEST4)
Using cmdSQL As New SqlCommand(strSQL, conn)
conn.Open()
rstData.Load(cmdSQL.ExecuteReader)
End Using
End Using
Return rstData
End Function
Note VERY carefull in above, I created a page wide (class wide) data table called rstRating. This will go out of scope after the data bind, but we ONLY need it to persit DURING the gv data bind operating (since for each row of the gv, we don't want to run that query over and over - we need this same pick list for the dll).
Ok, so now we see/get this:
The only part we need is to load up the dll, and set it for each row. We use the RowDataBound event.
So, code for that was this:
Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
' bind drop down for each row
If e.Row.RowType = DataControlRowType.DataRow Then
' get combo
Dim rDrop As DropDownList = e.Row.FindControl("cboRating")
rDrop.DataSource = rstRating
rDrop.DataBind()
rDrop.Items.Insert(0, New ListItem("Please Select", "0"))
' now get current row value for rating.
Dim gData As DataRowView = e.Row.DataItem
If IsDBNull(gData("Rating")) = False Then
rDrop.Text = gData("Rating")
End If
End If
End Sub
So, for each row, get the dll.
For for that row, load up with choices
And THEN for that row, set the ddl to the current row value (but check for null, and don't set - it will then show our "select" choice value.

Get row values programmatically from gridview

I have a GridView Control and one button:
<asp:GridView ID="grdView" runat ="server" AutoGenerateColumns ="false" >
<Columns>
<asp:TemplateField HeaderText ="Balance">
<ItemTemplate>
<%#Eval("Balance") %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button runat="server" ID ="btn" Text ="test"/>
Then on load page, I populate the gridView with a list of agreements. on that list there are one field called "Balance":
Private Sub form1_Load(sender As Object, e As EventArgs) Handles form1.Load
Dim agreementManager As AgreementManager = New AgreementManager()
Dim lstBalances As List(Of Agreement) = agreementManager.GetByClientId(2)
grdView.DataSource = lstBalances
grdView.DataBind()
End Sub
Then it display me this after loaded:
I am trying to read programmatically one specific balance with:
Private Sub btn_Click(sender As Object, e As EventArgs) Handles btn.Click
Dim value As String = grdView.Rows(1).Cells(0).Text
End Sub
But the "value" is empty.
What I am doing wrong?
I am working on mock a system that uses this way to read the values from a grid view, and this code works fine:
Dim balance As Decimal =
CType(grdApplyTransactionsAgreements.Rows(idx).Cells(BALANCE_CELLID).Text, Decimal)
this code is inside a button too.
Thanks!!
The .Text property can only be read after DataBinding from AutoGenerated columns and BoundField columns. But even if you could I would not recommend it since all you are getting is a string, not the original datatype.
Better read the values from the source lstBalances.
I bumped into an answer, I need to use BondField Control instead Template Field, now everything runs fine.
<asp:GridView ID="grdView" runat ="server" AutoGenerateColumns ="false" >
<Columns>
<asp:BoundField DataField ="Balance" HeaderText ="Balance"/>
</Columns>
</asp:GridView>

Editing GridView For dynamically generated Field

My GridView is as follows
It has EmptyDatatemplate and Command Field
<asp:GridView ID="AGridView" runat="server" AutoGenerateColumns="true" style="table-layout:fixed;" Width="2000px" RowStyle-HorizontalAlign="Left">
<EmptyDataTemplate>
</EmptyDataTemplate>
<asp:CommandField ShowEditButton="True" ItemStyle-Width="80px" EditText="Edit">
<ItemStyle Font-Bold="true" Font-Size="Small" />
<HeaderStyle CssClass="AAddOn" />
</asp:CommandField>
</asp:GridView>
GridView Looks like
Name Age Country
A 10 NNN Edit
B 23 NNN Edit
Now if i click on edit i need only Age Column to be edited
If i give AGridView.EditIndex = e.NewEditIndex entire row is edited
In codebehind
Private Sub AGridView_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles AGridView.RowEditing
Try
AGridView.EditIndex = e.NewEditIndex
AGridView.DataSource = SessionItems.ADataset.Tables("AHello")
AGridView.DataBind()
Catch ex As Exception
SetErrorMsg(ex.Message.ToString, "Error")
End Try
End Sub
That's simple. Whatever the columns you don't want editable, just add those in DataKeyNames property of grid. Try this.
<asp:GridView ID="AGridView" runat="server" AutoGenerateColumns="true" style="table-layout:fixed;" Width="2000px" RowStyle-HorizontalAlign="Left" DataKeyNames = "Name,Country" >
<EmptyDataTemplate>
</EmptyDataTemplate>
<asp:CommandField ShowEditButton="True" ItemStyle-Width="80px" EditText="Edit">
<ItemStyle Font-Bold="true" Font-Size="Small" />
<HeaderStyle CssClass="AAddOn" />
</asp:CommandField>
</asp:GridView>
Private Sub AGridView_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles AGridView.RowEditing
Try
Dim colsToNotEdit As List<string>
//C# code
for (int i = 0; i < AGridView.HeaderRow.Cells.Count; i++)
{
if(AGridView.HeaderRow.Cells[i].Text !="age")
colsToNotEdit.Add(grdToDisplay.HeaderRow.Cells[i].Text);
}
AGridView.DataKeyNames = colsToNotEdit.ToArray()
AGridView.EditIndex = e.NewEditIndex
AGridView.DataSource = SessionItems.ADataset.Tables("AHello")
AGridView.DataBind()
Catch ex As Exception
SetErrorMsg(ex.Message.ToString, "Error")
End Try
End Sub
Another option would be to not use the AutoGenerateColumns functionality and specify your column templates.
This way you can specify the ItemTemplate, EditTemplate or FooterTemplate for each column.
So for a column that you dont want to edit, ie. Name use <asp:BoundField> and for age use:
<asp:TemplateField>
<ItemTemplate><asp:Label>Bind Me</asp:Label></ItemTemplate>
<EditItemTemplate><asp:TextBox>Bind Me</asp:TextBox></EditItemTemplate>
</asp:TemplateField>
Edit: When using Dynamic Column Names
If you have a datatable that contains your data, lets say dtRowData.
You should have a piece of code where you assign the datatable to the grid:
GridView1.DataSource = dtRowData;
GridView1.DataBind();
You can add a loop in before the databind call as follows:
string DataKeyNames = "";
foreach (DataColumn dc in dtRowData.Columns)
{
if(dc.Name != "Age")
DataKeyNames += dc.Name + ",";
}
And then before Binding Set GridView1.DataKeyNames = DataKeyNames
You will probably need to do a substring on DataKeyNames to remove the trailing ,

Get a CheckBox to command a GridView row

I have a GridView holding my order table (these are inserted values from an insert statement). The order table appears on the doctor page, but I want to add to it so it can be approve/unapproved (if checked then update the 'Approve' column to approved, if not checked update the 'Approve' column to not approved. I need to add a CheckBox column to the GridView for this. This is part of my learning (not a live website).
How do I add a column of check boxes to a GridView that would set a row to approve/unapproved when checked?
This is my order table -(all the data is dummy data)
Grid:
<asp:GridView ID="GridViewdoc" runat="server" >
</asp:GridView>
Showing the data on the grid
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim conn As New System.Data.SqlClient.SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\surgerydb.mdf;Integrated Security=True;Connect Timeout=30")
Dim cmd3string As String = " Select * From alltheview WHERE DoctorId = " & Session("DoctorId")
Dim dt As New System.Data.DataTable()
Dim da As New System.Data.SqlClient.SqlDataAdapter(cmd3string, conn)
conn.Open()
da.Fill(dt)
conn.Close()
GridViewdoc.DataSource = dt
GridViewdoc.DataBind()
End If
End Sub
My select statement that pulls the data:
Create View theallview
As
Select A.OrderID
,A.PatientId ,B.Forename,B.Surname ,A.MedicineId ,C.Name as MedicineName ,E.Name as DoctorName, A.PharmacyId ,D.pharmname ,A.DoctorId ,A.Dateordered, Approved
From order_pres A
Left Join Patient B on (A.PatientId = B.PatientId)
Left Join Medicine C on (A.MedicineId = C.MedicineId)
Left Join pharmacy D on (A.PharmacyId = D.PharmacyId)
Left Join Doctor E on (A.DoctorId = E.DoctorId)
How the order table now looks (this is all dummy data by the way:
A button . click event will then submit the checked values
If anyone needs more information regarding this question please let me know.
Try this. It's in C# but you should be able to work out the VB.Net pretty easily.
As an aside I would put your code to load your data in its own method. Once the approved is updated I would recommend reloading your grid view.
ASPX:
<asp:GridView ID="GridViewdoc" DataKeyNames="OrderId" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:BoundField HeaderText ="Order Id" DataField="OrderId" />
<asp:BoundField HeaderText ="Patient Id" DataField="PatientId" />
<asp:BoundField HeaderText ="Medicine Id" DataField="MedicineId" />
<asp:BoundField HeaderText ="Pharmacy Id" DataField="PharmacyId" />
<asp:BoundField HeaderText ="Doctor Id" DataField="DoctorId" />
<asp:BoundField HeaderText ="Date Ordered" DataField="Dateordered" />
<asp:TemplateField HeaderText="Approve/Unapprove">
<ItemTemplate>
<asp:CheckBox ID="chkApproved" AutoPostBack="true" Checked='<%# Eval("Approved") == null || Eval("Approved") == DBNull.Value ? false : Eval("Approved") %>' runat="server" OnCheckedChanged="chkApproved_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code behind:
protected void chkApproved_CheckedChanged(object sender, EventArgs e)
{
CheckBox chkApproved = (CheckBox)sender;
GridViewRow gridViewRow = (GridViewRow)chkApproved.Parent.Parent;
int orderID = (int)GridViewdoc.DataKeys[gridViewRow.RowIndex].Value;
bool approved = chkApproved.Checked;
//Your update method
UpdateApproved(orderID, approved);
//Your data load method
LoadData();
}

Bind image in Gridview

I am using a gridview to display information on a page. The condition is that when I get Y from database result I need to bind /images/goldx.png Else /images/check.gif how can I do that I am using asp.net with vb.net as backend
<asp:GridView ID="grdLocation" runat="server" Width="100%" AutoGenerateColumns="false" >
<Columns>
<asp:TemplateField HeaderText="Monthly" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblLotName" runat="server" Text='<%#Eval("is_monthly") %>'></asp:Label>
<asp:Image ID="resultImage" runat="server" ImageUrl='<%# Eval("is_monthly") == 'Y' ? "~/Images/check.gif" : "~/Images/goldx.png" %>' />
</ItemTemplate>
</asp:TemplateField>
<Columns>
</asp:GridView>
My code for binding the Gridview :
Protected Function bindLocations()
Try
Dim _ds As DataSet
If _locComp Is Nothing Then
_locComp = New LocationComponent()
End If
_ds = _locComp.GetAllLots()
If _ds.Tables(0).Rows.Count > 0 Then
grdLocation.DataSource = _ds
grdLocation.DataBind()
End If
Catch ex As Exception
End Try
End Function
Thanks for your comment and answers .
You can do this check OnRowDataBound event of GridView. Like
Private Sub grdLocation_OnRowDataBound(sender As Object, e As GridViewRowEventArgs) Handles grdLocation.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim lblLotNametxt as String = CType(e.Row.FindControl("lblLotName"),Label).Text
If lblLotNametxt = "Y" Then
CType(e.Row.FindControl("resultImage"),Image).ImageUrl = "~/Images/check.gif"
Else
CType(e.Row.FindControl("resultImage"),Image).ImageUrl = "~/Images/goldx.png"
End If
End If
End Sub
Hope this helps you.

Resources