Solved

Trying to grab values in a gridview on editing

Posted on 2011-03-24
10
477 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
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…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

863 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

20 Experts available now in Live!

Get 1:1 Help Now