Solved

Trying to grab values in a gridview on editing

Posted on 2011-03-24
10
478 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
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

816 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