Solved

How to update a dataset row when user update a template field in  gridview

Posted on 2007-11-22
6
363 Views
Last Modified: 2012-06-21
Hello Experts,

I have a gridview with template fields(Dropdown) which is bound to a column in dataset. ... on a button click the record get added to a gridview... once the record is displayed in the gridview the user should be able to change the Method : to either Rnd or Pror.

How do I update my dataset based on user changes.

please guide.

I add the code... the first shows the gridview and its column

and the other one is click event of abutton that adds row to the dataset and then displayed in the gridview.


<asp:Panel ID="Panel1" runat="server" Height="136px" ScrollBars="Vertical" Style="z-index: 118;
                        left: 440px; position: absolute; top: 96px" Width="472px" BorderColor="Blue" BorderWidth="1px">
                        <asp:GridView ID="GridView1" runat="server"
                            Font-Names="Arial" Font-Size="7pt" Style="z-index: 100; left: 0px; position: absolute;
                            top: 0px" Width="456px" ShowHeader="False" AutoGenerateColumns="False" >
                                  <Columns>
                                  <asp:TemplateField>
                                    <ItemTemplate>
                                     &nbsp;<asp:Button ID="btnRowButton1" runat="server"   CommandName="Exclude" Text="x" Width="12px" Font-Names="Arial" Font-Size="7pt" Height="16px" style="text-align: center" />
                                        
                                    </ItemTemplate>
                                </asp:TemplateField>
                
                <asp:BoundField DataField="SrNo" SortExpression="Rank" ControlStyle-BorderWidth ="6px" >
                    <ControlStyle BorderWidth="6px" />
                </asp:BoundField>
                <asp:BoundField DataField="FSName" SortExpression="Rank" ControlStyle-BorderWidth ="20px">
                    <ControlStyle BorderWidth="20px" />
                </asp:BoundField>
                <asp:BoundField DataField="EffDt" SortExpression="Rank" ControlStyle-BorderWidth ="10px">
                    <ControlStyle BorderWidth="10px" />
                </asp:BoundField>
                <asp:TemplateField>
               <ItemTemplate>
               <asp:TextBox ID="txtPOC" runat="server" text='<%#DataBinder.Eval(Container.DataItem, "POC") %>'
Font-Names="Arial" Font-Size="7pt" Width="32px"></asp:TextBox>
               </ItemTemplate>
               </asp:TemplateField>
                                      <asp:TemplateField>
                                          <ItemTemplate>
                                              <asp:DropDownList ID="ddnPOS24" runat="server" SelectedValue='<%#DataBinder.Eval(Container.DataItem, "POS24") %>' Font-Names="Arial" Font-Size="7pt"
                                                  Width="48px">
                                                  <asp:ListItem Selected="True">Fac</asp:ListItem>
                                                  <asp:ListItem>NonFac</asp:ListItem>
                                              </asp:DropDownList>
                                          </ItemTemplate>
                                      </asp:TemplateField>
                                      <asp:TemplateField>
                                          <ItemTemplate>
                                              <asp:TextBox ID="txtAnesCF" runat="server" text='<%#DataBinder.Eval(Container.DataItem, "AnesCF") %>' Font-Names="Arial" Font-Size="7pt" Width="24px"></asp:TextBox>
                                          </ItemTemplate>
                                      </asp:TemplateField>
               <asp:TemplateField>
               <ItemTemplate>
               <asp:DropDownList ID="ddnAMethod" runat="server"  SelectedValue='<%#DataBinder.Eval(Container.DataItem, "AnesMethod") %>' Font-Names="Arial" Font-Size="7pt"
                Width="48px">
                <asp:ListItem Selected="True">Rnd</asp:ListItem>
               <asp:ListItem>Pror</asp:ListItem>
                   <asp:ListItem></asp:ListItem>
               </asp:DropDownList>
               </ItemTemplate>
                </asp:TemplateField>
                
            </Columns>
                        </asp:GridView>
                    </asp:Panel>
 
 
 
 
 
 
Protected Sub btnAdd1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd1.Click
        Dim dr As Data.DataRow
 
        i = i + 1
        dr = ds.Tables(0).NewRow
        dr("srno") = i
 
        If lstFS.SelectedValue <> "" Then
            Call FS_Detail(lstFS.SelectedValue)
            dr("FSName") = lstFS.SelectedValue
            dr("Type") = "NonOption"
            dr("effDT") = txtDate.Text
            dr("POC") = Session("POC")
            dr("POS24") = Session("POS24")
            dr("AnesCF") = Session("ACF")
            dr("AnesMethod") = "Pror"
        Else
            Call FS_Detail(getMKTID(lstMyFS.SelectedValue))
            dr("FSName") = lstMyFS.SelectedValue
            dr("Type") = "NonOption"
            dr("effDT") = txtDate.Text
            dr("POC") = Session("POC")
            dr("POS24") = Session("POS24")
            dr("AnesCF") = Session("ACF")
            dr("AnesMethod") = "Rnd"
        End If
 
        ds.Tables(0).Rows.Add(dr)
        dv = New DataView(ds.Tables(0))
        dv.RowFilter = "Type='NonOption'"
        Me.GridView1.DataSource = dv
        Me.GridView1.DataBind()
 
        For i = 0 To (lstFS.Items.Count - 1)
            lstFS.Items(i).Selected = False
        Next
        For i = 0 To (lstMyFS.Items.Count - 1)
            lstMyFS.Items(i).Selected = False
        Next
 
    End Sub

Open in new window

0
Comment
Question by:sanjaypandey
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 12

Expert Comment

by:Mortaza Doulaty
ID: 20343188
Change "Eval" in your code to Bind

For example for a text box you will have:
<asp:TextBox ID="txtPOC" runat="server" text='<%#DataBinder.Bind(Container.DataItem, "POC") %>'
Font-Names="Arial" Font-Size="7pt" Width="32px"></asp:TextBox>

Open in new window

0
 

Author Comment

by:sanjaypandey
ID: 20343860
Hello,

I get a error saying that bind is not a member of 'System.web.UI.DataBinder', Am I missing something?

0
 
LVL 12

Expert Comment

by:Mortaza Doulaty
ID: 20343943
This sample code may help:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="BlackListID"
            DataSourceID="AccessDataSource1">
            <Columns>
                <asp:BoundField DataField="BlackListID" HeaderText="BlackListID" InsertVisible="False"
                    ReadOnly="True" SortExpression="BlackListID" />
                <asp:BoundField DataField="Tel_vc" HeaderText="Tel_vc" SortExpression="Tel_vc" />
                <asp:BoundField DataField="Comments_vc" HeaderText="Comments_vc" SortExpression="Comments_vc" />
                <asp:TemplateField HeaderText="mrt">
                    <ItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Tel_vc") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

Open in new window

0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 12

Expert Comment

by:Mortaza Doulaty
ID: 20343954
Note that the last column is a template column that have a text box that binded to a filed using:
Text='<%# Bind("Tel_vc") %>'
0
 

Author Comment

by:sanjaypandey
ID: 20344063
will this automatically update the DataSet... ?  It does help in that on autopostback , the dropdown retains the value that it was changed to ... but when I submit the dataset to a table ..it shows the original value and not the one to which it was changed to..

    <asp:TemplateField>
                                          <ItemTemplate>
                                              <asp:DropDownList ID="ddnPOS24" runat="server" SelectedValue='<%#Bind("POS24") %>' Font-Names="Arial" Font-Size="7pt"
                                                  Width="40px"  AutoPostBack="True">
                                                  <asp:ListItem Selected="True">Fac</asp:ListItem>
                                                  <asp:ListItem>NonFac</asp:ListItem>
                                              </asp:DropDownList>
                                          </ItemTemplate>
                                      </asp:TemplateField>




This is how I read the dataset:


 For i = 97 To ds.Tables(0).Rows.Count - 1
            varstrSQL = varstrSQL + " Insert into " & varSelectedFSTblName & "(Fsname,Eff_DT, DEFPOC,POS, AnesCF,AnesMethod,SelectionType,ModelType)"
            varstrSQL = varstrSQL + "values( '" & ds.Tables(0).Rows(i).Item(2) & "','" & ds.Tables(0).Rows(i).Item(3) & "','" & ds.Tables(0).Rows(i).Item(4) & "','" & ds.Tables(0).Rows(i).Item(5) & "',"
            varstrSQL = varstrSQL + "'" & ds.Tables(0).Rows(i).Item(6) & "','" & ds.Tables(0).Rows(i).Item(7) & "','" & ds.Tables(0).Rows(i).Item(9) & "','" & ds.Tables(0).Rows(i).Item(8) & "')"
            cm.CommandText = varstrSQL
            cm.CommandType = CommandType.Text
            cm.ExecuteNonQuery()
            varstrSQL = ""
        Next
0
 
LVL 12

Accepted Solution

by:
Mortaza Doulaty earned 500 total points
ID: 20344950
You should set the update query of your datasource to that your changes in dataset take effect in database.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

710 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