Reputation: 693
I am trying to trying to allow the user to select multiple rows of my grid and then those selected values display in the text box. I can get one value into the textbox and able to select one row. How can I change what I already have to allow multiple selections? I tried whats posted below thinking adding a new instance of the object would allow me to see both values in the textbox but it just repeats the first selected value.
protected void dropdeadGridView_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
GridViewRow row = dropdeadGridView.Rows[e.NewSelectedIndex];
GridViewRow row1 = dropdeadGridView.Rows[e.NewSelectedIndex];
}
protected void dropdeadGridView_SelectedIndexChanged1(object sender, EventArgs e)
{
GridViewRow row = dropdeadGridView.SelectedRow;
GridViewRow row1 = dropdeadGridView.SelectedRow;
IDTextBox.Text = row.Cells[1].Text + "," + row1.Cells[1].Text;
loadnumTextBox.Text = row.Cells[2].Text + "," + row1.Cells[2].Text;
}
ASP code
<asp:GridView ID="dropdeadGridView" runat="server" Height="300px"
Width="650px" BackColor="Black" BorderStyle="Solid"
CellPadding="3" Font-Bold="True" ForeColor = "Yellow"
onselectedindexchanging="dropdeadGridView_SelectedIndexChanging"
autogenerateselectbutton="true" selectedindex="0"
onselectedindexchanged="dropdeadGridView_SelectedIndexChanged1" >
<RowStyle HorizontalAlign="Center" />
<SelectedRowStyle BorderStyle="Inset" BorderColor="Red" ForeColor="Red" />
</asp:GridView>
Upvotes: 0
Views: 5576
Reputation: 1627
Since you are using ASP.Net web forms I would recommend creating an placinig the check box control inside of there then the user clicks the checkbox on the rows they would like to select. In the code behind within the butto click event find the rows by looking for the checkbox.checked property. Afterwards read the desired cells data and append it to your textboxes.
<asp:GridView ID="yourGrid" runat="server" AutoGenerateColumns="False"
DataSourceID="YourSource" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="YourCheckBox" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="ModifiedDate" HeaderText="ModifiedDate" />
<asp:CommandField ShowEditButton="True" />
</Columns>
</asp:GridView>
If you are not comfy with JQuery then go with this.
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
CheckBox chk = (CheckBox)sender;
GridViewRow gr = (GridViewRow)chk.Parent.Parent;
txtBox.Text = GridView1.DataKeys[gr.RowIndex].Value.ToString();
}
However, I think the easiest way of doing this based on your requirements is by using JQuery.
First add a jquery script to your project. Go to Jquery.com pull down the latest script if your project does not have it already. In the head of your page mark up add the ref.
<script type="text/javascript" src="The source of your JS file"></script>
Then at the end of your body write your JQuery.
<script type="text/javascript">
$(document).ready(function () {
// Capture the click event by going through the gridview > tablerow > tabledisplay > `lastly the checkbox - grab the click event.
$('#GrdViewID' tr td
input[id*="chkSelected"]`[type=checkbox]:checked').on('click',function () {
$('#txtBox').text($(this).closest('tr').find('td').eq(2).text());
});
</script>
Upvotes: 2