Solved

Trying to grab values in a gridview on editing

Posted on 2011-03-24
10
476 Views
Last Modified: 2012-06-27
I am trying to grab the value from a column to be the selected value in a dropdownlist. I can't seem to get it to work. I am working for the first time in .net 4.0. So maybe that has something to do with it. Can anyone help? So I am trying to grab the value from the column that stores the NurseID. I believe it should be column 11.  Am I missing Something
<asp:GridView ID="grdAppointments" BorderStyle="None" 
                HorizontalAlign="Center" DataKeyNames="rowID" BackColor="#ffffff"
                Font-Size="10px" runat="server"
                AllowPaging="True" PageSize="100" AutoGenerateColumns="False" AllowSorting="True" AutoGenerateEditButton="true" OnRowEditing="grdAppointments_RowEditing" OnRowDataBound="grdAppointments_rowdatabound"
                OnRowCancelingEdit="grdAppointments_RowCancelling" OnRowUpdating="GridView1_RowUpdating" >
                <AlternatingRowStyle BackColor="Gainsboro" 
                    />
                <HeaderStyle BackColor="#666666" Font-Size="12px" ForeColor="White" 
                    HorizontalAlign="Center" />
                <PagerStyle HorizontalAlign="Center" ForeColor="White" BackColor="#666666" />
            <Columns>
                <asp:BoundField HeaderText="Apt Date" DataField="AppointmentDate" DataFormatString="{0:MM/dd/yyyy}"
                    ReadOnly="true" />
                <asp:BoundField HeaderText="Provider" DataField="Provider" ReadOnly="true" />
                <asp:BoundField HeaderText="MRN" DataField="MRN" ReadOnly="true" />
                <asp:BoundField HeaderText="Pt Name" DataField="PatientName" ReadOnly="true" />
                <asp:BoundField HeaderText="Apt Type" DataField="AppointmentType" ReadOnly="true" />
                <asp:BoundField HeaderText="Apt Time" DataField="AppointmentTime" ReadOnly="true" />
                <asp:TemplateField HeaderText="Status">
                    <ItemTemplate>
                        <asp:Label ID="lblNurse" runat="server" Text='<%#Eval("Status") %>'>
                        </asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlStatusEdit" runat="server">
                        </asp:DropDownList>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:BoundField HeaderText="Estimated time" DataField="StatusETA" ReadOnly="true" />
                <asp:TemplateField HeaderText="Assigned To">
                    <ItemTemplate>
                        <asp:Label ID="lblNurse" runat="server" Text='<%#Eval("Nurses") %>'>
                        </asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlNurseEdit" runat="server">
                        </asp:DropDownList>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Room">
                    <ItemTemplate>
                        <asp:Label ID="lblNurse" runat="server" Text='<%#Eval("Room") %>'>
                        </asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlRoomEdit" runat="server">
                        </asp:DropDownList>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:BoundField HeaderText="Apt Text" DataField="AppointmentText" ReadOnly="true" />
                <asp:BoundField HeaderText="Nurse" DataField="NurseID" Visible="false" ReadOnly="true" />
                <asp:BoundField HeaderText="Room" DataField="RoomID" Visible="false" ReadOnly="true" />
                <asp:BoundField HeaderText="Status" DataField="StatusID" Visible="false" ReadOnly="true" />
                 <asp:BoundField HeaderText="Row ID" DataField="rowID" ReadOnly="true" />
            </Columns>
                <SelectedRowStyle BackColor="#993333" ForeColor="White" Font-Bold="True" />
            </asp:GridView>


 string s = e.Row.Cells[15].Text;
                //categoryid = Int32.Parse(e.Row.Cells[0].Text);
                //expression = "categoryid = " + categoryid;
                DropDownList ddl = (DropDownList)e.Row.FindControl("ddlNurseEdit");
               ......
                ddl.SelectedValue = e.Row.Cells[11].Text;

Open in new window

0
Comment
Question by:NickMalloy
  • 6
  • 2
  • 2
10 Comments
 

Author Comment

by:NickMalloy
Comment Utility
The code should be for the rowbinding

 protected void grdAppointments_rowdatabound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            {
                string s = e.Row.Cells[15].Text;
                //categoryid = Int32.Parse(e.Row.Cells[0].Text);
                //expression = "categoryid = " + categoryid;
                DropDownList ddl = (DropDownList)e.Row.FindControl("ddlNurseEdit");
               ......
                ddl.SelectedValue = e.Row.Cells[11].Text;
               
            }
        }
    }
0
 
LVL 29

Expert Comment

by:Paul Jackson
Comment Utility
Better to bind the data directly in the asp:

<asp:DropDownList ID="ddlNurseEdit" runat="server" SelectedValue=<% #Eval ("Nurses")%>>
0
 

Author Comment

by:NickMalloy
Comment Utility
I tried
<asp:DropDownList ID="ddlNurseEdit" runat="server" SelectedValue='<%# Eval("NurseID") %>'

but it is throwing an error

'ddlNurseEdit' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
0
 
LVL 13

Accepted Solution

by:
gamarrojgq earned 250 total points
Comment Utility
Hi,

Try to use FindControl  but in the Cell that have the DLL, i believe that is cell 8

DropDownList ddl = (DropDownList)e.Row.Cells[8].FindControl("ddlNurseEdit");
0
 

Author Comment

by:NickMalloy
Comment Utility
My dropdownlist gets populated, I just can't select the current value. So I am able to find the control to place values in it.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 29

Expert Comment

by:Paul Jackson
Comment Utility
You need to populate the dropdownlist with some values, are the values for the dropdown list going to be populated from a database? Using a business Layer method call?

You need to setup a datasource to populate the dropdownlist values.

Using findcontrol and then setting the selectedvalue in the code behind if there are no values populated in the dropdown list
0
 

Author Comment

by:NickMalloy
Comment Utility
This is my full code. I populate the dropdowns and then I want to set the selected value based on another columns value.

protected void grdAppointments_rowdatabound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            {
                string s = e.Row.Cells[15].Text;
                //categoryid = Int32.Parse(e.Row.Cells[0].Text);
                //expression = "categoryid = " + categoryid;
                DropDownList ddl = (DropDownList)e.Row.FindControl("ddlNurseEdit");
                CPCCContext = new CPCCModel.CPCCEntities();
                var NurseQuery = from d in CPCCContext.tblNurses
                                      orderby d.Nurses
                                      select d;
                ddl.DataSource = ((ObjectQuery)NurseQuery).Execute(MergeOption.AppendOnly);
                ddl.DataTextField = "Nurses";
                ddl.DataValueField = "NurseID";
                ddl.DataBind();
                ddl.SelectedValue = e.Row.Cells[11].Text;
                var StatusQuery = from d in CPCCContext.tblStatus
                                  orderby d.Status
                                  select d;
                ddl = (DropDownList)e.Row.FindControl("ddlStatusEdit");
                ddl.DataSource = ((ObjectQuery)StatusQuery).Execute(MergeOption.AppendOnly);
                ddl.DataTextField = "Status";
                ddl.DataValueField = "StatusID";
                ddl.DataBind();
                ddl.SelectedValue = e.Row.Cells[13].Text;
                var RoomQuery = from d in CPCCContext.tblRooms
                                  orderby d.RoomID
                                  select d;
                ddl = (DropDownList)e.Row.FindControl("ddlRoomEdit");
                ddl.DataSource = ((ObjectQuery)RoomQuery).Execute(MergeOption.AppendOnly);
                ddl.DataTextField = "Room";
                ddl.DataValueField = "RoomID";
                ddl.DataBind();
                ddl.SelectedValue = e.Row.Cells[12].Text;
            }
        }
    }
0
 
LVL 13

Expert Comment

by:gamarrojgq
Comment Utility
Does it gives you an error message?
0
 

Author Comment

by:NickMalloy
Comment Utility
nope. just doesn't select the value. Say the nurseID column.
string s = e.Row.Cells[11].Text;
S has no value, but there is data in that column
0
 

Author Comment

by:NickMalloy
Comment Utility
Darn it. I had the wrong column number. I hate when that happens
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
ASP.net produce a pdf from a GridView 2 33
Gridview User Control event not firing 4 44
DLL in ASP.NET 20 38
Editing XSL files, 2 18
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now