?
Solved

Trying to grab values in a gridview on editing

Posted on 2011-03-24
10
Medium Priority
?
498 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
  • 2
10 Comments
 

Author Comment

by:NickMalloy
ID: 35210650
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
ID: 35210854
Better to bind the data directly in the asp:

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

Author Comment

by:NickMalloy
ID: 35210883
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Accepted Solution

by:
gamarrojgq earned 1000 total points
ID: 35210905
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
ID: 35210969
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
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 35210970
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
ID: 35210982
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
ID: 35210985
Does it gives you an error message?
0
 

Author Comment

by:NickMalloy
ID: 35211000
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
ID: 35211022
Darn it. I had the wrong column number. I hate when that happens
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

718 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