Posted by: R Manimaran | February 9, 2007

Scrollable Item in DataGrid

<!–StartFragment–><%@Page Language=”VB” Debug=”True” %>
<%@Import Namespace=”System.Data” %>

<%————————————————————-%>

<script runat=”server”>

Sub Page_Load()

  If Not Page.IsPostback Then
    dgr1.DataSource = GetDataTable()
    dgr1.DataBind()
  End If

End Sub

<%————————————————————-%>

Function GetDataTable() As DataTable

   ‘create a new empty DataTable object
   Dim oTable As New DataTable(“NewTable”)

   ‘define four columns (fields) within the table
   oTable.Columns.Add(“ISBN”, System.Type.GetType(“System.String”))
   oTable.Columns.Add(“Title”, System.Type.GetType(“System.String”))
   oTable.Columns.Add(“Precis”, System.Type.GetType(“System.String”))

   ‘create a new DataRow object instance in this table
   Dim oRow As DataRow = oTable.NewRow()

   ‘and fill in the values
   oRow(“ISBN”) = “1234567891”
   oRow(“Title”) = “Professional Application Center 2000”
   oRow(“Precis”) = “This book takes you through the complete process of setting up ” _
      & “and creating Web clusters and component clusters, adapting your applications to ” _
      & “work in a clustered scenario, and how you can administer and monitor them.”
   oTable.Rows.Add(oRow)

   oRow = oTable.NewRow()
   oRow(“ISBN”) = “1234567892”
   oRow(“Title”) = “A Preview of ASP.NET”
   oRow(“Precis”) = “ASP.NET makes it all much easier, much faster and less ” _
      & “error-prone to write your own Web applications and dynamic Web pages. In a lot ” _
      & “of cases, there is actually less code to write – and sometimes no code is required at all. ”
   oTable.Rows.Add(oRow)

   oRow = oTable.NewRow()
   oRow(“ISBN”) = “1234567893”
   oRow(“Title”) = “Professional ASP.NET XML Programming”
   oRow(“Precis”) = “As ASP.NET is probably the best server-side coding platform for ” _
      & “Windows-based systems, it’s an obvious choice for use with XML. This book looks ” _
      & “at all of the XML-related issues you will face when working in this environment.”
   oTable.Rows.Add(oRow)

   oRow = oTable.NewRow()
   oRow(“ISBN”) = “1234567894”
   oRow(“Title”) = “Beginning Components for ASP.NET”
   oRow(“Precis”) = “If you use ASP.NET, you’re only getting half the power of this ” _
      & “exciting programming environment if you don’t build and use components. This book ” _
      & “shows you how to get started, with plenty of tips, tricks and useful information.”
   oTable.Rows.Add(oRow)

  Return oTable

End Function

<%————————————————————-%>

Sub DoItemEdit(sender As Object, e As DataGridCommandEventArgs)

  ‘ set the EditItemIndex of the grid to this item’s index
  dgr1.EditItemIndex = e.Item.ItemIndex

  ‘ bind grid to display newly-loaded data
  dgr1.DataSource = GetDataTable()
  dgr1.DataBind()

End Sub

<%————————————————————-%>

Sub DoItemUpdate(sender As Object, e As DataGridCommandEventArgs)

  ‘ get a reference to the text boxes in edit row
  Dim oPrecis As TextBox = CType(e.Item.FindControl(“txtPrecis”), TextBox)

  ‘ create a suitable SQL statement and execute it
  Dim sSQL As String
  sSQL = “<b>SQL statement that could be executed is:</b></br />” _
       & “UPDATE BookDetails SET Precis='” & oPrecis.Text & “‘ ” _
       & “WHERE ISBN='” & sender.DataKeys(e.Item.ItemIndex) & “‘”
  lblMesg.text = sSQL

  ‘ set EditItemIndex of grid to -1 to switch out of Edit mode
  dgr1.EditItemIndex = -1

  ‘ bind grid to display row in new mode
  dgr1.DataSource = GetDataTable()
  dgr1.DataBind()

End Sub

<%————————————————————-%>

Sub DoItemCancel(sender As Object, e As DataGridCommandEventArgs)

  ‘ set EditItemIndex of grid to -1 to switch out of Edit mode
  dgr1.EditItemIndex = -1

  ‘ bind grid to display row in new mode
  dgr1.DataSource = GetDataTable()
  dgr1.DataBind()

End Sub

</script>

<%————————————————————-%>
<%————————————————————-%>

<html>
<head>
<!– #include file=”../global/style.inc” –>
<title>Scrollable Content in DataGrid Cells</title>

</head>
<body>

<span class=”heading”>Scrollable Content in DataGrid Cells</span><hr />

<form runat=”server”>

  <asp:DataGrid id=”dgr1″ runat=”server”
       Font-Size=”10″ Font-Name=”Tahoma,Arial,Helvetica,sans-serif”
       BorderStyle=”None” BorderWidth=”1px” BorderColor=”#deba84″
       BackColor=”#DEBA84″ CellPadding=”5″ CellSpacing=”1″
       DataKeyField=”ISBN”
       OnEditCommand=”DoItemEdit”
       OnUpdateCommand=”DoItemUpdate”
       OnCancelCommand=”DoItemCancel”
       AutoGenerateColumns=”False”>
    <HeaderStyle Font-Bold=”True” ForeColor=”#ffffff” BackColor=”#b50055″ />
    <ItemStyle BackColor=”#FFF7E7″ VerticalAlign=”Top” />
    <AlternatingItemStyle BackColor=”#ffffc0″ />

    <Columns>

      <asp:BoundColumn DataField=”ISBN” HeaderText=”ISBN”
           HeaderStyle-HorizontalAlign=”Left” ReadOnly=”True”
           ItemStyle-Width=”100px”
           ItemStyle-VerticalAlign=”Top” />

      <asp:TemplateColumn HeaderText=”Precis”
           HeaderStyle-HorizontalAlign=”Left”>
        <ItemTemplate>
          <div style=”width:300;height:70;overflow:scroll;
                      border:1 solid black;padding:2;
                      background-color:white”>
            <%# Container.DataItem(“Precis”) %>
          </div>
        </ItemTemplate>
        <EditItemTemplate>
          <asp:Textbox id=”txtPrecis” runat=”server”
               TextMode=”MultiLine” Rows=”4″
               Width=”300″ Style=”width:300px”
               Text='<%# Container.DataItem(“Precis”) %>’ />
        </EditItemTemplate>
      </asp:TemplateColumn>

      <asp:EditCommandColumn EditText=”Edit”
           CancelText=”Cancel” UpdateText=”Update”
           ItemStyle-Width=”90px”
           ItemStyle-VerticalAlign=”Top” />

    </Columns>
  </asp:DataGrid>

  <p />
  <asp:Label id=”lblMesg” EnableViewState=”False” runat=”server” />
</form>

<hr />
<!– #include file=”../global/viewsource.inc” –>
<!– #include file=”../global/footer.inc” –>

</body>
</html>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: