how can i have a DetailsView convert Latitude & longitude from floating point (eg: 123.5) to degrees.minutes (eg: 123.30)?

haw can i have a DetailsView convert Latitude & longitude from floating point (eg: 123.5) to degrees.minutes (eg: 123.30)?  i have only a fairly basic understanding of ASP.NET.  

the conversion from 123.5 to 123.30 (and "back" for saving) is not a problem.  

my question is about intercepting that data before it is displayed so we can change it.  for saving, we need to intercept it again and convert it back to floating point for saving...

thank you for your help!
LVL 2
mikepjAsked:
Who is Participating?
 
CmdoProg2Commented:
in the ItemUpdating you can use e.NewValues("LatLong") instead of e.NewValues(3) for example.

or depending on your data source
myId = CType(dvAssignmentDetails.DataItem, Data.DataRowView).Row("id")

0
 
CmdoProg2Commented:
Within your Itemcreated event, find the control with the Lat/Long and run your conversion.  For more on this check http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.itemcreated(VS.80).aspx

You can change the value during the ItemUpdating event of the DetailsView  
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.itemupdating(VS.80).aspx
0
 
mikepjAuthor Commented:
thank you for your reply; that's a good step forward.  something that's holding me up is that i dislike referring to fields by ordinal number.  i'd like to find them by name but i haven't found a way to get the DetailsView to let me find the field by name.  do you know the answer to that?
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
mikepjAuthor Commented:
DetailsView.ItemCreated Event only occurs when a record is created in a DetailsView control.  it fires but is blank (it's as though it's firing before data binding).
0
 
CmdoProg2Commented:
My Bad,  This is an example for a string in your databind item ... the DetailsView

      <asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True" DataSourceID="SqlDataSource1" OnDataBound="DetailsView1_DataBound" AutoGenerateRows="False" DataKeyNames="weaponTypeID">
        <Fields>
          <asp:BoundField DataField="weaponTypeID" HeaderText="weaponTypeID" InsertVisible="False" ReadOnly="True" SortExpression="weaponTypeID" />
          <asp:TemplateField HeaderText="weaponType" SortExpression="weaponType">
            <EditItemTemplate>
              <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("weaponType") %>'></asp:TextBox>
            </EditItemTemplate>
            <InsertItemTemplate>
              <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("weaponType") %>'></asp:TextBox>
            </InsertItemTemplate>
            <ItemTemplate>
              <asp:Label ID="WeaponType" runat="server" Text='<%# Bind("weaponType") %>'></asp:Label>
            </ItemTemplate>
          </asp:TemplateField>
        </Fields>

  protected void DetailsView1_DataBound(object sender, EventArgs e)
  {
    //DataRowView drv = (DataRowView)DetailsView1.DataItem;
    // Conversion here on  drv.Row["WeaponType"]
    Label lbl = (Label)DetailsView1.FindControl("WeaponType");
    lbl.Text = "Fred";
  }
0
 
mikepjAuthor Commented:
thank you for your help!  it works; can you review my work?

although this works, it's longer & more convoluted than i had expected.  am i doing this correctly?  what i'm wondering about is the use of edtLatitude1and edtLatitude2, etc.  seems like i might be over-doing this a bit.  your opinion.

      protected void dvBlock_DataBound(object sender, EventArgs e)
      {
         DataRowView drv = (DataRowView) dvBlock.DataItem;

         double dLat;

         if (drv.Row.IsNull("Latitude"))
            dLat = 0;
         else
            dLat = (double)drv["Latitude"];

         string sLat = Lib.JRPUtils.LatLongToStr(dLat);

         Label lbl = (Label)dvBlock.FindControl("lblLatitude");
         if (lbl != null)
            lbl.Text = sLat;

         TextBox edt = (TextBox)dvBlock.FindControl("edtLatitude1");
         if (edt != null)
            edt.Text = sLat;

         edt = (TextBox)dvBlock.FindControl("edtLatitude2");
         if (edt != null)
            edt.Text = sLat;

         double dLong;

         if (drv.Row.IsNull("Longitude"))
            dLong = 0;
         else
            dLong = (double)drv["Longitude"];

         string sLong = Lib.JRPUtils.LatLongToStr(dLong);

         lbl = (Label)dvBlock.FindControl("lblLongitude");
         if (lbl != null)
            lbl.Text = sLong;

         edt = (TextBox)dvBlock.FindControl("edtLongitude1");
         if (edt != null)
            edt.Text = sLong;

         edt = (TextBox)dvBlock.FindControl("edtLongitude2");
         if (edt != null)
            edt.Text = sLong;
      }

      protected void dvBlock_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
      {
         e.NewValues["Latitude"] = Lib.JRPUtils.StrToLatLong((string) e.NewValues["Latitude"]);
         e.NewValues["Longitude"] = Lib.JRPUtils.StrToLatLong((string) e.NewValues["Longitude"]);
      }



   public class JRPUtils
   {
      public static string LatLongToStr(double d)
      {
         if (d > 0)
         {
            int iDeg = (int)d;
            int iMin = (int)((d - iDeg) * 60);

            return string.Format("{0}.{1:00}", iDeg.ToString(), iMin);
         }
         else
         {
            return "";
         }
      }

      public static double StrToLatLong(string s)
      {
         s.Trim();

         if (s == "") return 0;

         double d = double.Parse(s);

         int iDeg = (int) d;
         double dMin = (d - iDeg) / 0.60;

         return iDeg + dMin;
      }
   }



  <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label><asp:DetailsView ID="dvBlock" runat="server" AutoGenerateRows="False" CellPadding="4"
      DataKeyNames="BlockID,OwnerID" DataSourceID="dsBlock" DefaultMode="Edit" ForeColor="#333333"
      GridLines="None" Height="50px" Width="125px" OnDataBound="dvBlock_DataBound" OnItemUpdating="dvBlock_ItemUpdating" >
      <Fields>
         <asp:TemplateField HeaderText="Latitude" SortExpression="Latitude">
            <EditItemTemplate>
               <asp:TextBox ID="edtLatitude1" runat="server" Text='<%# Bind("Latitude") %>'></asp:TextBox>
            </EditItemTemplate>
            <InsertItemTemplate>
               <asp:TextBox ID="edtLatitude2" runat="server" Text='<%# Bind("Latitude") %>'></asp:TextBox>
            </InsertItemTemplate>
            <ControlStyle Width="50px" />
            <ItemTemplate>
               <asp:Label ID="lblLatitude" runat="server" Text='<%# Bind("Latitude") %>'></asp:Label>
            </ItemTemplate>
         </asp:TemplateField>
         <asp:TemplateField HeaderText="Longitude" SortExpression="Longitude">
            <EditItemTemplate>
               <asp:TextBox ID="edtLongitude1" runat="server" Text='<%# Bind("Longitude") %>'></asp:TextBox>
            </EditItemTemplate>
            <InsertItemTemplate>
               <asp:TextBox ID="edtLongitude2" runat="server" Text='<%# Bind("Longitude") %>'></asp:TextBox>
            </InsertItemTemplate>
            <ControlStyle Width="50px" />
            <ItemTemplate>
               <asp:Label ID="lblLongitude" runat="server" Text='<%# Bind("Longitude") %>'></asp:Label>
            </ItemTemplate>
         </asp:TemplateField>
      </Fields>
   </asp:DetailsView>
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.