Adjust DetailsView edit TextBox widths to fit data definition

protected void DetailsView1_DataBound(object sender, System.EventArgs e)
{
 if (sender.CurrentMode == DetailsViewMode.Edit) {
  foreach (DetailsViewRow TrimDetailsViewRow in sender.Rows) {
   foreach (DataControlFieldCell TrimDataControlFieldCell in TrimDetailsViewRow.Cells) {
    Int64 MaxColumnWidth = new Int64();
    if (TrimDataControlFieldCell.ContainingField is BoundField) {
     BoundField WidthBoundField = TrimDataControlFieldCell.ContainingField;
     string WidthFieldName = WidthBoundField.DataField;
     if (sender.DataItem is Data.DataRowView) {
      Data.DataRowView WidthDataRowView = sender.DataItem;
      Data.DataView WidthDataView = WidthDataRowView.DataView;
      Data.DataTable WidthDataTable = WidthDataView.Table;
      Data.DataColumn WidthColumn = WidthDataTable.Columns(WidthFieldName);
      MaxColumnWidth = WidthColumn.MaxLength;
     }
    }
    if (TrimDataControlFieldCell.HasControls == true) {
     foreach (Control TrimControl in TrimDataControlFieldCell.Controls) {
      if (TrimControl is TextBox) {
       TextBox TrimTextBox = TrimControl;
       TrimTextBox.Text = Strings.Trim(TrimTextBox.Text);
       if (Information.IsDate(TrimTextBox.Text) == true) {
        MaxColumnWidth = 27;
       }
       if (MaxColumnWidth > 0) {
        if (MaxColumnWidth > 253) {
         TrimTextBox.Width = new WebControls.Unit("97%");
        } else {
         TrimTextBox.Width = new WebControls.Unit(MaxColumnWidth + 2 + "ex");
        }
       }
      }
     }
    }
   }
  }
 }
}

No comments:

Post a Comment