Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting data from gridview

Status
Not open for further replies.

kss444

Programmer
Sep 19, 2006
306
US
I have the following grid veiw. InstanceId is a primarykey in my DB. I am also doing asp:BoundField on this file but visibility is false. When a user checks the checkbox of the gridview I want to capture all the rest of the data columbs to be inserted into another talbe.
My question is how do I get the values of the selecte rows?
The latest thing I have been using is DataKeyNames.
Any help would be great. Thanks.

<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" EmptyDataText="No customers found." DataKeyNames="InstanceId">
<Columns>

<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="InstanceId" HeaderText="InstanceId" SortExpression="InstanceId"
Visible="False" />
<asp:BoundField DataField="CustomerName" HeaderText="Customer Name" SortExpression="CustomerName" />
<asp:BoundField DataField="InvoiceNumber" HeaderText="Invoice Number" SortExpression="InvoiceNumber" />
<asp:BoundField DataField="OutstandingAmount" HeaderText="Outstanding Amount" SortExpression="OutstandingAmount" HtmlEncode="false" DataFormatString="{0:c}" />
<asp:BoundField DataField="OriginalAmount" HeaderText="Orig Amount" SortExpression="OriginalAmount" HtmlEncode="false" DataFormatString="{0:c}" />
<asp:BoundField DataField="AmountPaid" HeaderText="Amount Paid" SortExpression="AmountPaid" HtmlEncode="false" DataFormatString="{0:c}" />

</Columns>
</asp:GridView>

Ordinary Programmer
 
Code:
<asp:GridView ID="GridView1" runat="server" 
    AutoGenerateColumns="False" DataKeyNames="InstanceId"> 
    <Columns>
        <asp:TemplateField> 
            <ItemTemplate> 
                <asp:CheckBox ID="chkSelect" runat="server" Checked='<%#Bind("MyBoolField") %>' /> 
            </ItemTemplate> 
        </asp:TemplateField> 
        <asp:BoundField DataField="CustomerName" HeaderText="Customer Name" SortExpression="CustomerName" /> 
        <asp:BoundField DataField="InvoiceNumber" HeaderText="Invoice Number" SortExpression="InvoiceNumber" /> 
        <asp:BoundField DataField="OutstandingAmount" HeaderText="Outstanding Amount" SortExpression="OutstandingAmount" HtmlEncode="false" DataFormatString="{0:c}"  /> 
        <asp:BoundField DataField="OriginalAmount" HeaderText="Orig Amount" SortExpression="OriginalAmount" HtmlEncode="false" DataFormatString="{0:c}"  /> 
        <asp:BoundField DataField="AmountPaid" HeaderText="Amount Paid" SortExpression="AmountPaid" HtmlEncode="false" DataFormatString="{0:c}"  /> 
    </Columns> 
</asp:GridView>
<asp:Button id="MyButton" runat="server" text="Save Selected Records" Click="MyButton_Click" />
Code:
protected void MyButton_Click(object sender, EventArgs e)
{
   IList<int> selectedIds = new List<int>();
   DropDownList ddl;
   foreach(GridViewRow row in GridView1.Rows)
   {
      ddl = (DropDownList)row.FindControl("chkSelect");
      if (ddl != null)
      {
          if (ddl.Checked)
          {
             selectedIds.Add((int)GridView1.DataKeys[row.RowIndex]["InstanceId"];
          }
      }
   }
   //use selectedIds to save records.
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top