且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用下拉列表使用另一个表中的值更新gridview

更新时间:2023-01-07 09:42:56

你需要使用网格的RowCommand事件并在代码隐藏文件中写入更新逻辑。


< asp:gridview id =GridView1datasourceid =MyDataSourcedatakeynames =代码xmlns:asp =#unknown>

AutoGenerateColumns =falserunat =server>

< columns> < asp:commandfield buttontype =Linkshowsitbutton =trueshowcancelbutton =true>

< asp:boundfield datafield =Codeheadertext =Code>

< asp:boundfield datafield =Nameheadertext =Name>

< asp:boundfield datafield =描述headertext =Description>





< asp:sqldatasource id =MyDataSourcexmlns:asp =#unknown>

ConnectionString =<%


Connectionstrings:ERPConnectionString%>

SelectCommand =SELECT * FROM Sample

UpdateCommand =更新样本集名称= @名称,描述= @描述其中代码= @代码

DeleteCommand =删除代码=代码='代码runat =server/>



--------上面的方法就是当你在网格中分离出句法时,如果你没有在c#代码中进行任何编码你应该使用的东西类似于上面的一个。请注意'


I am using ASP.Net 4.0 in Visual Studio 2010. I have two tables in my database 'registration'- table1 ==" Users" which has a column called usertype and table 2=="Requests" which has a column called assigned_to.

What I intend to do is:- when the
page loads the grid view is shown and when I click the edit command, the assigned_to column contains a dropdown list showing all the values from the firstname column of Users where Usertype='facilitator'.

Now when I click the value in the drop down list and then click update the values in the grid view do not get updated. I am not using any code in the C# file. I am just using the edit template feature of the grid view.

please could any body help?

You need to use RowCommand event of the grid and write the update logic in code behind file.


<asp:gridview id="GridView1" datasourceid="MyDataSource" datakeynames="Code" xmlns:asp="#unknown">
AutoGenerateColumns="false" runat="server">
<columns> <asp:commandfield buttontype="Link" showeditbutton="true" showcancelbutton="true">
<asp:boundfield datafield="Code" headertext="Code">
<asp:boundfield datafield="Name" headertext="Name">
<asp:boundfield datafield="Description" headertext="Description">


<asp:sqldatasource id="MyDataSource" xmlns:asp="#unknown">
ConnectionString="<%


Connectionstrings:ERPConnectionString%>"
SelectCommand="SELECT * FROM Sample"
UpdateCommand="Update SAMPLE SET Name=@Name,Description=@Description Where Code=@Code"
DeleteCommand="Delete SAMPLE Where Code=@Code" runat="server"/>

-------- The above way is when you sepcify soruce also in the grid, From your question if you are not doing any coding in c# Code you should be using something similar to one above. Please note the '