How to Display the search Data in GridView ? - asp.net

My GridViews which are connected to their respective LinqDataSource(LinqDataSourceMale & LinqDataSourceFemale) are
<asp:GridView ID="GridViewMale" runat="server" AllowPaging="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="ID" ForeColor="Black" GridLines="Horizontal" PageSize="5" Width="692px" DataSourceID="LinqDataSourceMale">
<Columns>
<asp:ImageField DataImageUrlField="Image" NullImageUrl="images/bullet.png" ReadOnly="True">
</asp:ImageField>
<asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Education" HeaderText="Education" SortExpression="Education" />
<asp:BoundField DataField="CurrentStatus" HeaderText="CurrentStatus" SortExpression="CurrentStatus" />
<asp:BoundField DataField="Height" HeaderText="Height" SortExpression="Height" />
<asp:BoundField DataField="Complexion" HeaderText="Complexion" SortExpression="Complexion" />
<asp:BoundField DataField="Caste" HeaderText="Caste" SortExpression="Caste" />
<asp:BoundField DataField="Group" HeaderText="Group" SortExpression="Group" />
</Columns>
<FooterStyle BackColor="#CCCC99" ForeColor="Black" />
<HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F7F7F7" />
<SortedAscendingHeaderStyle BackColor="#4B4B4B" />
<SortedDescendingCellStyle BackColor="#E5E5E5" />
<SortedDescendingHeaderStyle BackColor="#242121" />
</asp:GridView>
<asp:LinqDataSource ID="LinqDataSourceMale" runat="server" ContextTypeName="MB.BerouDataContext" EntityTypeName="" TableName="Males">
</asp:LinqDataSource>
<asp:GridView ID="GridViewFemale" runat="server" AllowPaging="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="ID" ForeColor="Black" GridLines="Horizontal" PageSize="5" Width="693px" DataSourceID="LinqDataSourceFemale">
<Columns>
<asp:ImageField DataImageUrlField="Image" >
</asp:ImageField>
<asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Education" HeaderText="Education" SortExpression="Education" />
<asp:BoundField DataField="CurrentStatus" HeaderText="CurrentStatus" SortExpression="CurrentStatus" />
<asp:BoundField DataField="Height" HeaderText="Height" SortExpression="Height" />
<asp:BoundField DataField="Complexion" HeaderText="Complexion" SortExpression="Complexion" />
<asp:BoundField DataField="Caste" HeaderText="Caste" SortExpression="Caste" />
<asp:BoundField DataField="Group" HeaderText="Group" SortExpression="Group" />
</Columns>
<FooterStyle BackColor="#CCCC99" ForeColor="Black" />
<HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Center" Wrap="True" />
<SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F7F7F7" />
<SortedAscendingHeaderStyle BackColor="#4B4B4B" />
<SortedDescendingCellStyle BackColor="#E5E5E5" />
<SortedDescendingHeaderStyle BackColor="#242121" />
</asp:GridView>
<asp:LinqDataSource ID="LinqDataSourceFemale" runat="server" ContextTypeName="MB.BerouDataContext" EntityTypeName="" TableName="Females">
</asp:LinqDataSource>
My SearchClick event code is:
protected void ButtonSearch_Click(object sender, EventArgs e)
{
using(BerouDataContext Data = new BerouDataContext())
{
if (DropDownListGender.SelectedItem.Text == "Male")
{
int age = Convert.ToInt32(DropDownListAge.Text);
string education = DropDownListEducation.Text.ToString();
string maritalstatus = DropDownListMaritalStatus.Text.ToString();
string caste = DropDownListCaste.Text.ToString();
string city = DropDownListCity.ToString();
var SearchResultBoys = Data.Males.Where(tan =>
(tan.Age == age)
&& (tan.Education.Contains(education))
&& (tan.Group.Contains(maritalstatus))
&& (tan.Caste.Contains(caste)));
GridViewMale.DataSourceID = "";
GridViewMale.DataSource = SearchResultBoys;
GridViewMale.DataBind();
}
else if (DropDownListGender.SelectedItem.Text == "Female")
{
int age = Convert.ToInt32(DropDownListAge.Text);
string education = DropDownListEducation.Text.ToString();
string maritalstatus = DropDownListMaritalStatus.Text.ToString();
//var religion = DropDownListReligion.Text.ToString();
string caste = DropDownListCaste.Text.ToString();
string city = DropDownListCity.ToString();
var SearchResultGirls = Data.Females.Where(tan =>
(tan.Age == age)
&& (tan.Education.Contains(education))
&& (tan.Group.Contains(maritalstatus))
&& (tan.Caste.Contains(caste)));
GridViewFemale.DataSourceID = "";
GridViewFemale.DataSource = SearchResultGirls;
GridViewFemale.DataBind();
}
}
}
Iam unable to display the data in gridview, gridview doesnt apper after searchClickEvent, please Help.

I think you might need to re-initlize your LinqDataSource data source instead of gridview
Take a look at this tutorial and this thread

Related

Dinamically created checkboxes, event handler and more - vb.NET asp.NET

I'm trying to manage the checkall control in a gridview but I'm facing several problems.
This is my gridview control:
<asp:GridView ID="gvShow" runat="server" AutoPostback="True" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" Height="224px" HorizontalAlign="Center" Width="761px" CellPadding="4" ForeColor="#333333" DataSourceID="sid_db">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField ItemStyle-Width="50px">
<HeaderTemplate>
<asp:CheckBox ID="chkCheckAll" runat="server" AutoPostBack="False" OnCheckedChanged="chkCheckAll_CheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkCheck" runat="server" />
</ItemTemplate>
<ItemStyle Width="50px"></ItemStyle>
</asp:TemplateField>
<asp:BoundField DataField="Scope" HeaderText="Scope" SortExpression="Scope" />
<asp:BoundField DataField="Brand" HeaderText="Brand" SortExpression="Brand" />
<asp:BoundField DataField="Site ID" HeaderText="Site ID" SortExpression="Site ID" />
<asp:BoundField DataField="Site Name" HeaderText="Site Name" SortExpression="Site Name" />
<asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />
<asp:BoundField DataField="CAP" HeaderText="CAP" SortExpression="CAP" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
<asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" />
<asp:BoundField DataField="Master_ID" HeaderText="Master_ID" SortExpression="Master_ID" ItemStyle-CssClass="hiddencol" HeaderStyle-CssClass="hiddencol">
<HeaderStyle CssClass="hiddencol"></HeaderStyle>
<ItemStyle CssClass="hiddencol"></ItemStyle>
</asp:BoundField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
First problem:
The checkbox controls are dynamically created and I honestly have the feeling that I should create an event handler for those, but I don't know how to do it nor I know where to put/write that code. I just see those controls are not recognized once I try to reference them in my code.
Second problem:
Since I'm pretty much a newbie, I made a step back and tested with some junk code if I was able to have all the checkboxes dinamically checked at page load... here is my test... the result a complete failure, nothing happens. I even tried to put the code in the init event, but the result is the same.
For Each row As GridViewRow In gvShow.Rows
DirectCast(row.FindControl("chkCheck"), CheckBox).Checked = True
Next
I would be grateful if anyone could help me with these problems.
Thanks in advance for your time.
Regards,
I have found the answer myself. I had to change the code as follow:
<asp:GridView ID="gvShow" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" Height="224px" HorizontalAlign="Center" Width="761px" CellPadding="4" ForeColor="#333333">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField ItemStyle-Width="50px">
<HeaderTemplate>
<asp:CheckBox ID="ChkSelectAll" runat="server" Autopostback="True" OnCheckedChanged="ChkSelectAll_CheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="ChkSigleItem" runat="server" OnCheckedChanged="ChkSigleItem_CheckedChanged" />
</ItemTemplate>
<ItemStyle Width="50px"></ItemStyle>
</asp:TemplateField>
<asp:BoundField DataField="Scope" HeaderText="Scope" SortExpression="Scope" />
<asp:BoundField DataField="Brand" HeaderText="Brand" SortExpression="Brand" />
<asp:BoundField DataField="Site ID" HeaderText="Site ID" SortExpression="Site ID" />
<asp:BoundField DataField="Site Name" HeaderText="Site Name" SortExpression="Site Name" />
<asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />
<asp:BoundField DataField="CAP" HeaderText="CAP" SortExpression="CAP" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
<asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" />
<asp:BoundField DataField="Master_ID" HeaderText="Master_ID" SortExpression="Master_ID" ItemStyle-CssClass="hiddencol" HeaderStyle-CssClass="hiddencol">
<HeaderStyle CssClass="hiddencol"></HeaderStyle>
<ItemStyle CssClass="hiddencol"></ItemStyle>
</asp:BoundField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
And then:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Page.IsPostBack = False Then
''Set value for default SQL query
Dim query As String
query = "SELECT Sites.Master_ID AS Master_ID, Scopes.Name AS Scope, Brands.Extension AS [Brand], Sites.ID AS [Site ID], Sites.Name AS [Site Name], Sites.Address, Sites.CAP, Sites.City, Countries.Name AS Country FROM Sites INNER JOIN Scopes ON Sites.scope_ID = Scopes.ID INNER JOIN Brands ON Sites.brand_ID = Brands.ID INNER JOIN Countries ON Sites.country_ID = Countries.ID"
sid_db.SelectCommand = query
gvShow.DataSource = sid_db
gvShow.DataBind()
End If
End Sub
Protected Sub ChkSelectAll_CheckedChanged(sender As Object, e As EventArgs)
Dim a As Boolean = DirectCast(sender, CheckBox).Checked
For Each row As GridViewRow In gvShow.Rows
Dim cbx As CheckBox = row.FindControl("ChkSigleItem")
cbx.Checked = a
Next
End Sub

Incorrect syntax near 'int' when using updatecommand on a gridview

I have a problem that when ever I try to update a record in the database using gridview update event handler and an updatecommand in sqldatascource
I get the following exception
Incorrect syntax near 'int'.
the weird thing is when I reload the page I find that the record was updated successfully but yet I need this exception message not to appear what should I do
update event handler
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex];
int BusId = Convert.ToInt32(((DataKey)GridView1.DataKeys[e.RowIndex]).Value);
TextBox Capacity =(TextBox) row.Cells[1].Controls[0];
TextBox DriverName =(TextBox) row.Cells[3].Controls[0];
TextBox Phone =(TextBox) row.Cells[4].Controls[0];
SqlDataSource1.UpdateParameters.Add("capaity", Capacity.Text);
SqlDataSource1.UpdateParameters.Add("id", BusId.ToString());
SqlDataSource1.UpdateParameters.Add("driver", DriverName.Text);
SqlDataSource1.UpdateParameters.Add("phone", Phone.Text);
SqlDataSource1.Update();
}
gridview asp.net code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="Bus Number" DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None" Height="326px" Width="1017px" OnRowUpdating="GridView1_RowUpdating">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="Bus Number" HeaderText="Bus Number" ReadOnly="True" SortExpression="Bus Number" />
<asp:BoundField DataField="Capacity" HeaderText="Capacity" SortExpression="Capacity" />
<asp:BoundField DataField="NumberOfBooking" HeaderText="NumberOfBooking" SortExpression="NumberOfBooking" ReadOnly="True" />
<asp:BoundField DataField="Driver" HeaderText="Driver" SortExpression="Driver" />
<asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" ShowHeader="False" />
<asp:BoundField DataField="Date Added" HeaderText="Date Added" SortExpression="Date Added" DataFormatString="{0:MM/dd/yyyy}" ReadOnly="True" />
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
the updatecommand
UpdateCommand="UPDATE Buses SET Capacity = #capaity WHERE VechileId = #id ;UPDATE Vechile SET Driver = #driver, Phone = #phone
WHERE VechileId = #id">

show data in gridview after selection from dropdown list

Hi i just want to show data in my gridview after selecting value from dropdown list and after selecting my dropdown list when i press on search button data can be seen in gridview. My gridview is connected with my database how can i do that my code is this:
aspx page:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="#333333"
GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Portal" HeaderText="Portal"
SortExpression="Portal" />
<asp:BoundField DataField="TID" HeaderText="TID" SortExpression="TID" />
<asp:BoundField DataField="PNR" HeaderText="PNR" SortExpression="PNR" />
<asp:BoundField DataField="TicketNumber" HeaderText="TicketNumber"
SortExpression="TicketNumber" />
<asp:BoundField DataField="ESACCode" HeaderText="ESACCode"
SortExpression="ESACCode" />
<asp:BoundField DataField="WaiverCode" HeaderText="WaiverCode"
SortExpression="WaiverCode" />
<asp:BoundField DataField="Remarks" HeaderText="Remarks"
SortExpression="Remarks" />
<asp:BoundField DataField="UnusedTicketAmount" HeaderText="UnusedTicketAmount"
SortExpression="UnusedTicketAmount" />
<asp:BoundField DataField="ddlUnusedAmount" HeaderText="ddlUnusedAmount"
SortExpression="ddlUnusedAmount" />
<asp:BoundField DataField="AirlinePenality" HeaderText="AirlinePenality"
SortExpression="AirlinePenality" />
<asp:BoundField DataField="ddlAirlinePenality" HeaderText="ddlAirlinePenality"
SortExpression="ddlAirlinePenality" />
<asp:BoundField DataField="NetRefundProcess" HeaderText="NetRefundProcess"
SortExpression="NetRefundProcess" />
<asp:BoundField DataField="ddlNetRefundProcess"
HeaderText="ddlNetRefundProcess" SortExpression="ddlNetRefundProcess" />
<asp:BoundField DataField="RefundableCommission"
HeaderText="RefundableCommission" SortExpression="RefundableCommission" />
<asp:BoundField DataField="ddlRefundableCommission"
HeaderText="ddlRefundableCommission" SortExpression="ddlRefundableCommission" />
<asp:BoundField DataField="CouponRefunded" HeaderText="CouponRefunded"
SortExpression="CouponRefunded" />
<asp:BoundField DataField="RefundType" HeaderText="RefundType"
SortExpression="RefundType" />
<asp:BoundField DataField="DateTime" HeaderText="DateTime"
SortExpression="DateTime" />
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:BartConnectionString %>"
SelectCommand="SELECT * FROM [Test]"></asp:SqlDataSource>
code on aspx.cs page
public void GridViewBind()
{
SqlCommand cmd = new SqlCommand("select * from Test where ID=" + DropDownList1.SelectedValue + "", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "Test");
GridView1.DataSource = ds.Tables["Test"];
GridView1.DataBind();
}
protected void btnSearch_Click(object sender, EventArgs e)
{
this.GridViewBind();
}
After selecting value from my dropdown list Error : Invalid column name 'TRAVELONG'
Is ID a string? If so, you need inner quotes for the ID field:
SqlCommand cmd = new SqlCommand("select * from Test where ID='" + DropDownList1.SelectedValue + "'", con);
Otherwise, it may be considering the selected value as a column...

How to make a Gridview template field select the row of data?

I have a command field in a gridview, that as it is, works as it should. When i select it, the row is hilighted. However, i needed to convert this field into a template, so i could give it a ID that i could reference when using a AJAX Mobal control. I did this, and i am able to reference it with my Ajax control just fine, but, now this field does not select the row in the Gridview? The Select value is needed for the Ajax control to pull the data thru. So i think my issue is, How do i use this command field as a template, AND have it select the row? I hope i am explaining my issue correctly.
Here is the snippet of code when i convert it into a template.
Thank you!
<asp:GridView ID="GridView3" runat="server" AllowPaging="True" AutoGenerateColumns="False" CellPadding="3" DataKeyNames="Contact_ID" DataSourceID="SqlDataSource2" ForeColor="#333333" GridLines="None" PageSize="6">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Select" Text="Edit"></asp:LinkButton>
<asp:ModalPopupExtender ID="ModalPopupExtender2" runat="server" TargetControlID="LinkButton1" PopupControlID="DetailsView1" BackgroundCssClass="modalBackground"></asp:ModalPopupExtender>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Contact_ID" HeaderText="Contact_ID" InsertVisible="False" ReadOnly="True" SortExpression="Contact_ID" Visible="False" />
<asp:BoundField DataField="Contact_Assigned_Username" HeaderText="Username" SortExpression="Contact_Assigned_Username" />
<asp:BoundField DataField="Contact_First_Name" HeaderText="First Name" SortExpression="Contact_First_Name" />
<asp:BoundField DataField="Contact_Last_Name" HeaderText="Last Name" SortExpression="Contact_Last_Name" />
<asp:BoundField DataField="Contact_Email_Address" HeaderText="Email Address" SortExpression="Contact_Email_Address" />
<asp:BoundField DataField="Contact_Cell_Phone" HeaderText="Cell Phone" SortExpression="Contact_Cell_Phone" />
<asp:CheckBoxField DataField="Contact_Administrator" HeaderText="Admin" SortExpression="Contact_Administrator" />
<asp:CheckBoxField DataField="Contact_LineStat_Triggers_Email" HeaderText="Email Triggers" SortExpression="Contact_LineStat_Triggers_Email" />
<asp:CheckBoxField DataField="Contact_LineStat_Triggers_Text" HeaderText="Text Triggers" SortExpression="Contact_LineStat_Triggers_Text" />
<asp:CheckBoxField DataField="Contact_Web_Portal" HeaderText="Web Access" SortExpression="Contact_Web_Portal" />
<asp:BoundField DataField="Contact_Customer_ID" HeaderText="Contact_Customer_ID" SortExpression="Contact_Customer_ID" Visible="False" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
In the RowDataBound event, try something like this:
protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
{
LinkButton btn = (LinkButton)e.Row.FindControl("LinkButton1");
btn.OnClientClick = Page.ClientScript.GetPostBackEventReference(GridView3, "Select$" + e.Row.RowIndex.ToString()));
}

How do I access the text of a selected BoundColumn of a DataGrid?

<asp:DataGrid ID="dgResetPassword" DataKeyField="user_id" OnItemCommand="resetSelect" CellPadding="10" HeaderStyle-BorderStyle="none" AutoGenerateColumns="False" runat="server" ForeColor="#333333" GridLines="None" Width="550px">
<Columns>
<asp:ButtonColumn DataTextField="sap_id" HeaderText="SAP ID" />
<asp:BoundColumn DataField="lastname" HeaderText="Last Name" />
<asp:BoundColumn DataField="firstname" HeaderText="First Name"/>
<asp:BoundColumn DataField="username" HeaderText="User Name"/>
<asp:BoundColumn DataField="jobtitle" HeaderText="Job Title"/>
<asp:BoundColumn DataField="orgunit" HeaderText="Organization Unit"/>
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditItemStyle BackColor="#999999" />
<SelectedItemStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<AlternatingItemStyle BackColor="White" ForeColor="#284775" />
<ItemStyle BackColor="#F7F6F3" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" BorderStyle="None" Font-Bold="True" ForeColor="White" />
</asp:DataGrid>
That is the grid, here is the function...
Sub resetSelect(ByVal s As Object, ByVal e As DataGridCommandEventArgs)
lblResponse.Text = [want to access text of username here]
lblResponse.Visible = True
lblID.Text = dgResetPassword.DataKeys.Item(e.Item.ItemIndex)
End Sub
How do I reference the selected items DataField text for username?
username is in the 3rd column. So you can do this (C#):
lblResponse.Text = e.Item.Cells[3].Text;
This will be the VB.net way of doing it.
lblResponse.Text = e.Item.Cells(3).Text
It may be beneficial for you to set up an enum that represents the columns and their indices. As such your code wiould become
lblResponse.Text = e.Item.Cells(DataColumns.Username).Text

Resources