Click here to Skip to main content
15,921,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
To get the ListBox selected Items, i wrote like this . simillarly instead of ListBox i'm using GridView.How to write the same code.In Girdview i have some rows i want to read some of them and store it into a string datatype.

C#
ListBox LstBox = (dgi.FindControl("lstType") as ListBox);
string strselect = "";
for (int j = 0; j < LstBox.Items.Count; j++)
{
     ListItem li = LstBox.Items[j];
     if (li.Selected)
     {
        strselect = strselect + ", " + li.Value;
     }
}
Posted
Updated 17-Sep-12 21:03pm
v2
Comments
[no name] 18-Sep-12 9:12am    
It is WinForm or Asp.Net?

C#
foreach (DataGridViewRow row in gdv.Rows)
{
string col1Val = row.Cells[0].Value.ToString();
string col2Val =  row.Cells[1].Value.ToString();
}
 
Share this answer
 
Hello !

you can use template fields and add a named command to it then you can check which of them was clicked in the RowCommand event (and u can also get the row index as well) see below.

in .aspx page:

XML
<asp:TemplateField ShowHeader="False">
            <ItemTemplate>
                <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="false"
                    CommandName="MyCommand" Text="Button" CommandArgument='<%# Container.DataItemIndex %>'></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>


in .aspx.cs page on RowCommand Event:

C#
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
            {
                if(e.CommandName.Equals("MyCommand"))
                {
                    int row = Int32.Parse(e.CommandArgument.ToString());


                }


            }


Thanks.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900