Gridview format field as a phone number, but some results are 4-bit extensions, how to handle?

So, I am showing the results from an SQL table in a gridview. some fields are phone numbers. A specific field can be a normal 10-digit number, but it can also be a four-digit extension. If this is a 4-digit number, I want to at least not put 10-digit formatting on it, maximum, I would like to add it and say Ext: followed by my data. Here is what I still have. I'm usually not a programmer, so these are Visual Studio wizards and the Google results put together. Thanks so much for any help.

<form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="False"> <Columns> <asp:TemplateField HeaderText="Call Destination" SortExpression="CallDestination"> <EditItemTemplate> <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("CallDestination") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label2" runat="server" Text='<%# String.Format("{0:(###) ###-####}",Convert.ToInt64(DataBinder.Eval (Container.DataItem, "CallDestination")))%>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:OnCallConnectionString %>" SelectCommand="SELECT [TimeStamp], [CallerID], [Accepted], [CallDestination] FROM [OnCallLog]"></asp:SqlDataSource> </div> </form> 
+4
source share
1 answer

You need to use the RowDataBound event to intercept each row, since it is tied to the grid, so you can determine if the phone number is 10 digits or 4 digits and process each value in each case, for example:

Markup:

 <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="False" onrowdatabound="GridView1_RowDataBound"> 

Note. Remove Text='<%# String.Format("{0:(###) ###-####}",Convert.ToInt64(DataBinder.Eval (Container.DataItem, "CallDestination")))%>' from <asp:Label> to <ItemTemplate> because you format the text and set the Text property to RowDataBound instead of declarative.

Code for:

 protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e) { // Only interested in each data row, not header or footer, etc. if(e.Row.RowType == DataControlRowType.DataRow) { // Find the Label2 control in the row Lable theLabel = (Label)e.row.FindControl("Label2"); // Make sure control is not null if(theLabel != null) { // Cast the bound to an object we can use to extract the value from DataRowView rowView = (DataRowView)e.Row.DataItem; // Get the value for CallDestination field in data source string callDestinationValue = rowView["CallDestination"].ToString(); // Find out if CallDestination is 10 digits or 4 digits if(callDestinationValue.Length == 10) { theLabel.Text = String.Format("{0:(###) ###-####}", Convert.ToInt64(rowView["CallDestination"])); } if(callDestinationValue.Length == 4) { theLabel.Text = "Ext: " + callDestinationValue; } } } } 
+1
source

Source: https://habr.com/ru/post/1499401/


All Articles