在GridView中选择行时将值传递到相应的字段 [英] Passing values to respective fields on selecting a Row in GridView

查看:50
本文介绍了在GridView中选择行时将值传递到相应的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我一直在尝试并试图解决这个问题,但无济于事。因此,我在这里发帖。请注意,我是一个自学者,并不深入了解事情,只是想学习..所以请温柔。这是我的aspx代码:

Ok so i have been trying and trying to solve this issue, but to no avail. Therefore, i am posting here. Please note that i am a self learner and don't really know things in depth, just trying to learn.. So please be gentle. Here is my aspx code:

<div class="gview">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CssClass="gview" DataKeyNames="ID" DataSourceID="SqlDataSource1" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowSelectButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="PName" HeaderText="PName" SortExpression="PName" />
<asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" />
<asp:BoundField DataField="ContactNum" HeaderText="ContactNum" SortExpression="ContactNum" />
<asp:BoundField DataField="Gender" HeaderText="Gender" SortExpression="Gender" />
<asp:BoundField DataField="TestName" HeaderText="TestName" SortExpression="TestName" />
<asp:BoundField DataField="ReqDate" HeaderText="ReqDate" SortExpression="ReqDate" />
<asp:BoundField DataField="RepDate" HeaderText="RepDate" SortExpression="RepDate" />
<asp:BoundField DataField="Consultant" HeaderText="Consultant" SortExpression="Consultant" />
</Columns>
</asp:GridView>
<div id="labels" >
<asp:Label ID="Label1" runat="server" Text="Name:"></asp:Label>
<asp:Label ID="Label2" runat="server" Text="Gender:"></asp:Label>
<asp:Label ID="Label3" runat="server" Text="Age:"></asp:Label>
<asp:Label ID="Label4" runat="server" Text="Contact Number:"></asp:Label>
<asp:Label ID="Label5" runat="server" Text="Requesting Date:"></asp:Label>
<asp:Label ID="Label6" runat="server" Text="Report Date:"></asp:Label>
<asp:Label ID="Label7" runat="server" Text="Test Name:"></asp:Label>
<asp:Label ID="Label8" runat="server" Text="Consultant:"></asp:Label>
<asp:Label ID="Label9" runat="server" Text="ID:"></asp:Label>
</div>
<br />
<div id="textboxes">
<asp:TextBox ID="TextBox1" runat="server" Height="33px" Width="270px"></asp:TextBox>
<asp:DropDownList ID="DropDownList1" runat="server" Height="25px" Width="270px">
<asp:ListItem>Female</asp:ListItem>
<asp:ListItem>Male</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="TextBox2" runat="server" Height="33px" Width="270px"></asp:TextBox>
<asp:TextBox ID="TextBox3" runat="server" Height="31px" Width="270px"></asp:TextBox>
<asp:TextBox ID="TextBox4" runat="server" Height="28px" Width="270px"></asp:TextBox>
<asp:CalendarExtender ID="TextBox4_CalendarExtender" runat="server" Enabled="True" TargetControlID="TextBox4"> </asp:CalendarExtender>
<asp:TextBox ID="TextBox5" runat="server" Height="33px" Width="270px"></asp:TextBox>
<asp:CalendarExtender ID="TextBox5_CalendarExtender" runat="server" Enabled="True" TargetControlID="TextBox5">
</asp:CalendarExtender>
<asp:ComboBox ID="ComboBox1" runat="server" MultiSelect="true" Height="25px" Width="270">
<asp:ListItem>ANA</asp:ListItem>
<asp:ListItem>ASMA</asp:ListItem>
<asp:ListItem>ASO-titres</asp:ListItem>
<asp:ListItem>ESR</asp:ListItem>
<asp:ListItem>CBC</asp:ListItem>
<asp:ListItem>Anti-double Stranded DNA ab</asp:ListItem>
<asp:ListItem>Urea</asp:ListItem>
<asp:ListItem>T3, T4, TSH</asp:ListItem>
<asp:ListItem>Uric Acid</asp:ListItem>
<asp:ListItem>Totel Iron binding</asp:ListItem>
<asp:ListItem>Lipid profile</asp:ListItem>
<asp:ListItem>INR</asp:ListItem>
<asp:ListItem>LFT's</asp:ListItem>
<asp:ListItem>RA Factor</asp:ListItem>
<asp:ListItem>Serum Iron</asp:ListItem>
<asp:ListItem>Serum Ferritin</asp:ListItem>
<asp:ListItem>Calcium, Phosphates</asp:ListItem>
</asp:ComboBox>
<asp:TextBox ID="TextBox6" runat="server" Height="25px" Width="270px"></asp:TextBox>
<asp:TextBox ID="TextBox7" runat="server" Height="25px" Width="270px"></asp:TextBox>





What i am trying to do is, when i click on



What i am trying to do is, when i click on

Select (Auto Generated by SQL)





all the data in that particular row should be fetched in the respective fields for editing purposes, so that the user doesn’t have to retype all the info. (textboxes, dropdownlist etc)



This is what i have done so far: (only giving the needed code)



all the data in that particular row should be fetched in the respective fields for editing purposes, so that the user doesn't have to retype all the info. (textboxes, dropdownlist etc)

This is what i have done so far: (only giving the needed code)

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
int i = Convert.ToInt32(e.CommandArgument);
TextBox1.Text = GridView1.Rows[i].Cells[1].Text.ToString();
DropDownList1.SelectedItem.Text = GridView1.Rows[i].Cells[2].Text.ToString();
TextBox2.Text = GridView1.Rows[i].Cells[3].Text.ToString();
TextBox3.Text = GridView1.Rows[i].Cells[4].Text.ToString();
TextBox4.Text = GridView1.Rows[i].Cells[5].Text.ToString();
TextBox5.Text = GridView1.Rows[i].Cells[6].Text.ToString();
ComboBox1.Text = GridView1.Rows[i].Cells[7].Text.ToString();
TextBox6.Text = GridView1.Rows[i].Cells[8].Text.ToString();
TextBox7.Text = GridView1.Rows[i].Cells[0].Text.ToString();
}
}





Now IDK if this is right or not, but i learned it somewhere and tried to implement.. but to no avail. Any kind of help will be greatly appreciated. P.S.:The event is fired when i click on select, the page refreshes.



Now IDK if this is right or not, but i learned it somewhere and tried to implement.. but to no avail. Any kind of help will be greatly appreciated. P.S.:The event is fired when i click on select, the page refreshes.

推荐答案

int rowIndex = int.Parse(e.CommandArgument.ToString());
string val = (string)this.grid.DataKeys[rowIndex]["myKey"];


这篇关于在GridView中选择行时将值传递到相应的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆