Solved

Trying to grab values in a gridview on editing

Posted on 2011-03-24
10
490 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 13

Accepted Solution

by:
gamarrojgq earned 250 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

627 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