[英]How do I allow edit only a particular column in datagridview in windows application?
[英]How and Where do i enter the code to edit just one column of a datagridview
好的,所以我已经创建了datagridview并启用了对列的编辑,如下图所示。 接下来,我要为编辑链接编写一个click事件,以便在单击该事件时,仅investigatorID列可用于编辑。 在那种情况下,当可以进行更改时,我希望调查者列显示可以选择的调查者下拉列表,并将所选项目绑定到单击了编辑链接的行。
我有显示调查者ID的数据源,只是想知道在哪里编写代码(即使是编辑链接也可以单击),并使其发挥作用。
让工作一些魔术师.. :)然后我再三考虑,因为我只需要将调查员ID传递到数据库中进行更新,便有了一个更好的主意,我按如下方式编辑设计(请参见下图),因此在网格视图中显示展览类型和图像后,我宁愿只调用将分配调查者ID的展览ID,当您查看下面的数据库时,这样做更有意义:
尽管没有完全填充,但是数据库背后的概念是:1。 一个案例可能与一个以上的展览相关联(也就是说,一个案例包含多个展览,并且每个展览都分配有一位调查人员,而该调查人员一次不能处理三个以上的展览。
因此,在此阶段,当调查人员分配给案例时,已经填充了caseID和Exhibit details。 打开案件的管理员已经在那里有他们的ID,尚未分配案件的经理将有他们的ID和要分配给字段的调查员ID。
看看我的第二个设计,我已经能够创建一个网格以仅显示正在处理2个或更少展览的调查员。 这意味着可以将它们分配给至少一个以上的展览。 为了进行分配,我从下拉列表中选择一个CaseID,该列表详细列出了案例表中所有案例(此处未显示),并基于所选案例,显示了所有链接到所选casedID的展览ID。 从那里,用户可以从下拉列表中选择一个展览,然后选择要分配的任何可用调查员。 因此,我现在要做的就是用户单击分配按钮时,我想知道在哪里以及如何使单击分配按钮所依据的调查者ID作为更新以及该gridvire(用户ID,请重新命名用户名,也就是调查者列表),这样,如果最近单击的调查者ID达到其三个展览的阈值,则不会显示其调查者ID和名称。 同时,已分配的展览品也不会再次出现在下拉列表中。 因此,实际上,单击了分配按钮,并将其旁边的ID发送到展览表以进行更新,并且刷新了gridview和展览下拉列表。
网格视图应该没有问题,因为它的数据直接绑定了,所以一个简单的Exhibitiongridview.databind()应该可以解决这个问题,并且可能调用先前填充的expiredropdownlist的方法也可以对dropdownlist进行刷新...
因此,现在的问题是如何获取分配按钮单击背后的代码以使此事件发生。 请在所附代码后找到用于母版页的代码(使用嵌套母版),以及应在当前页面上使用的代码和.cs文件的代码。
第一个母版页(SITE.MASTER)后面的代码
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="Prototype5.SiteMaster" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head id="Head1" runat="server">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
// $(function () {
// $("#showOpenCasePage").click(function () {
// $("#CaseMainPanel").load("/EntryForms/OpenCase.aspx");
// });
// $("#showAddExhibitPage").click(function () {
// $("#CaseMainPanel").load("/EntryForms/AddExhibit.aspx");
// });
// $("#showOpenCasePage").click(function () {
// $("#CaseManagementMainPanel").load("/EntryForms/OpenCase.aspx");
// });
// $("#showAddExhibitPage").click(function () {
// $("#CaseManagementMainPanel").load("/EntryForms/AddExhibit.aspx");
// });
// $("#showAllocateOfficerPage").click(function () {
// $("#CaseManagementMainPanel").load("/EntryForms/AllocateOfficer.aspx");
// });
// $("#showReallocateOfficerPage").click(function () {
// $("#CaseManagementMainPanel").load("/EntryForms/ReallocateOfficer.aspx");
// });
// $("#showPrioritizeCasePage").click(function () {
// $("#CaseManagementMainPanel").load("/EntryForms/CasePriority.aspx");
// });
// $("#showRecordFindingPage").click(function () {
// $("#InvestigatorMainPanel").load("/EntryForms/RecordFinding.aspx");
// });
// $("#showUpdateStatusPage").click(function () {
// $("#InvestigatorMainPanel").load("/EntryForms/UpdateStatus.aspx");
// });
// });
</script>
<title></title>
<link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
<asp:ContentPlaceHolder ID="HeadContent" runat="server">
</asp:ContentPlaceHolder>
<script type="text/javascript">
function updateClock() {
var currentTime = new Date();
var currentHours = currentTime.getHours();
var currentMinutes = currentTime.getMinutes();
var currentSeconds = currentTime.getSeconds();
// Pad the minutes and seconds with leading zeros, if required
currentMinutes = (currentMinutes < 10 ? "0" : "") + currentMinutes;
currentSeconds = (currentSeconds < 10 ? "0" : "") + currentSeconds;
// Choose either "AM" or "PM" as appropriate
var timeOfDay = (currentHours < 12) ? "AM" : "PM";
// Convert the hours component to 12-hour format if needed
currentHours = (currentHours > 12) ? currentHours - 12 : currentHours;
// Convert an hours component of "0" to "12"
currentHours = (currentHours == 0) ? 12 : currentHours;
// Compose the string for display
var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;
// Update the time display
document.getElementById("clock").firstChild.nodeValue = currentTimeString;
}
</script>
<style type="text/css">
.style1
{
width: 213px;
}
.menu
{}
.style2
{
width: 100%;
}
.style3
{
width: 264px;
}
.style4
{
width: 291px;
}
</style>
</head>
<body onload="updateClock(); setInterval('updateClock()', 1000 )">
<form id="Form1" runat="server">
<div class="page">
<div class="header">
<div class="title">
<span style="font-size: 5em; font-family:Old English Text MT; color: #FF6600;"><strong>Case Management System</strong></span>
</div>
<div class="clock">
<span id="clock"> </span>
</div>
<div class="clear hideSkiplink">
<table class="style2">
<tr>
<td class="style4" align="center">
<asp:LoginView ID="MasterLoginView" runat="server">
<LoggedInTemplate>
Welcome <span class="bold"><asp:LoginName ID="HeadLoginName" runat="server" />
[ <asp:LoginStatus ID="HeadLoginStatus" runat="server" LogoutAction="Redirect" LogoutText="Log Out" LogoutPageUrl="~/Logout.aspx"/> ]
<%--Welcome:
<span class="bold"><asp:LoginName ID="MasterLoginName" runat="server" /> </span>!--%>
</LoggedInTemplate>
<AnonymousTemplate>
Welcome: Guest
[ <a href="Account/Login.aspx" ID="HeadLoginStatus" runat="server">Log In</a> ]
</AnonymousTemplate>
</asp:LoginView>
</td>
<td class="style3">
</td>
<td valign="top">
<asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="False"
IncludeStyleBlock="False" Orientation="Horizontal" Height="36px" Width="120px">
<Items>
<asp:MenuItem NavigateUrl="~/Default.aspx" Text="Home"
ImageUrl="~/Images/homeIcon.png"/>
<asp:MenuItem NavigateUrl="~/About.aspx" Text="About"
ImageUrl="~/Images/aboutIcon.png"/>
<asp:MenuItem ImageUrl="~/Images/contactUsIcon.png" NavigateUrl="~/ContactUs.aspx"
Text="Contact Us" Value="Contact Us"></asp:MenuItem>
</Items>
</asp:Menu>
</td>
</tr>
</table>
</div>
</div>
</div>
<div class="page" style="margin-top:5px;height:auto;">
<div style="border-style:solid;">
<table style="width:100%; background-color:#3a4f63">
<tr>
<td class="style1" valign="top">
<p style="padding-left: 4px; padding-right:4px;">
<asp:Button ID="functionButton" runat="server" Text="System Functions"
class="fnctButton" Height="41px" Width="192px" />
<asp:ContentPlaceHolder ID="LeftNavigation" runat="server">
</asp:ContentPlaceHolder>
</p>
</td>
<td rowspan="2" valign="top" align="center">
<asp:ContentPlaceHolder ID="MainContent" runat="server"/>
</td>
</tr>
<tr>
<td class="style1" align="left" valign="top">
<asp:ContentPlaceHolder ID="RightNewsItem" runat="server"/>
</td>
</tr>
</table>
</div>
</div>
<div class="clear">
</div>
<div class="footer">
<span style="font-size: small;color: #FFFFFF;"><strong>Copyright 2011 JustRite Software Inc.</strong></span></div>
</form>
</body>
</html>
基于第一个母版页的第二个母版页(Manage.Master)
<%@ Master Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Manager.master.cs" Inherits="Prototype5.Manager" %>
<asp:Content ID="ManagerHead" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="ManagerLeft" ContentPlaceHolderID="LeftNavigation" runat="server">
<div style="margin-top:20px; margin-bottom:20px;">
<p class="actionButton">
<asp:Button ID="allocateInvestigatorsButton" runat="server"
Text="Allocate Investigators" height="30px" width="135px"
onclick="allocateInvestigatorsButton_Click" />
</p>
<p class="actionButton">
<asp:Button ID="realocateInvestigatorsButton" runat="server"
Text="Reallocate Investigators" Height="30px" Width="135px"
onclick="realocateInvestigatorsButton_Click" />
</p>
<p class="actionButton">
<asp:Button ID="prioritizeCaseButton" runat="server"
Text="Prioritize Case" height="30px" width="135px"
onclick="prioritizeCaseButton_Click" />
</p>
<p class="actionButton">
<asp:Button ID="openCaseButton" runat="server"
Text="Open Case" height="30px" width="135px"
onclick="openCaseButton_Click" />
</p>
<p class="actionButton">
<asp:Button ID="registerExhibitButton" runat="server"
Text="Register Exhibit" height="30px" width="135px"
onclick="registerExhibitButton_Click" />
</p>
</div>
</asp:Content>
<asp:Content ID="ManagerMain" ContentPlaceHolderID="MainContent" runat="server">
<asp:ContentPlaceHolder ID="MainContent" runat="server"/>
</asp:Content>
<asp:Content ID="ManagerRight" ContentPlaceHolderID="RightNewsItem" runat="server">
<asp:Button ID="AlertButton" runat="server" Text="Alerts"
class="fnctButton" Height="25px" Width="70px" />
<div style="border-style: none; border-color: inherit; border-width: medium; width:210px; height: 103px;">
<p style="text-align: justify; font-size:1.2em; color:White; width: 209px; height: 95px;">
This is a place holder for allerts about cases to which the investigator has been assigned to.
</p>
</div>
</asp:Content>
当前页面包含基于第二母版页的元素
<%@ Page Title="" Language="C#" MasterPageFile="~/Manager.master" AutoEventWireup="true" CodeBehind="AllocateInvestigators.aspx.cs" Inherits="Prototype5.AllocateInvestigators" %>
<asp:Content ID="AllocateInvestigatorsMain" ContentPlaceHolderID="MainContent" runat="server">
<div>
<table class="style2">
<tr>
<td colspan="4" style="background-color: #C0C0C0">
</td>
</tr>
<tr>
<td class="style1"
style="width: 79px; color: #000000; background-color: #666666;">
<strong>Case ID:</strong></td>
<td align="left" class="style1" style="width: 187px">
<asp:DropDownList ID="caseIDDropDownList" runat="server" Height="22px"
Width="116px" DataSourceID="distinctCaseIDSqlDataSource"
DataTextField="CaseID" DataValueField="CaseID"
onselectedindexchanged="caseIDDropDownList_SelectedIndexChanged">
</asp:DropDownList>
<asp:SqlDataSource ID="distinctCaseIDSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:CMSSQL3ConnectionString1 %>" SelectCommand="SELECT DISTINCT CaseID FROM Exhibits
WHERE InvestigatorID = NULL"></asp:SqlDataSource>
</td>
<td class="style3"
style="width: 119px; color: #000000; background-color: #666666;">
<strong>Case Priority:</strong></td>
<td align="left">
<asp:DropDownList ID="casePriorityDropDownList" runat="server" height="22px"
width="116px">
<asp:ListItem>Low</asp:ListItem>
<asp:ListItem>Medium</asp:ListItem>
<asp:ListItem>High</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td colspan="4" style="background-color: #C0C0C0">
</td>
</tr>
</table>
</div>
<div>
<table class="style2">
<tr>
<td style="width: 79px; color: #000000;">
<strong>Exhibit ID:</strong></td>
<td align="left">
<asp:DropDownList ID="exhibitsDropDownList" runat="server" height="22px"
onselectedindexchanged="exhibitsDropDownList_SelectedIndexChanged"
width="116px">
</asp:DropDownList>
<asp:SqlDataSource ID="exhibitIDSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:CMSSQL3ConnectionString1 %>"
SelectCommand="SELECT ExhibitID FROM Exhibits"></asp:SqlDataSource>
</td>
</tr>
<tr>
<td colspan="2">
</td>
</tr>
</table>
</div>
<div>
<table class="style2">
<tr>
<td>
<asp:GridView ID="exhibitGridView" runat="server" AutoGenerateColumns="False"
CellPadding="4" DataKeyNames="UserID" DataSourceID="SqlDataSource1"
EmptyDataText="There are no data records to display." ForeColor="#333333"
GridLines="None" Height="241px"
onselectedindexchanged="GridView1_SelectedIndexChanged" Width="323px"
style="margin-right: 0px">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="UserID" HeaderText="UserID" ReadOnly="True"
SortExpression="UserID" />
<asp:BoundField DataField="UserName" HeaderText="UserName"
SortExpression="UserName" />
<asp:ButtonField ButtonType="Button" CommandName="Select" Text="Assign" />
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>
<asp:SqlDataSource ID="assignInvestigatorsSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:CMSSQL3ConnectionString1 %>"
SelectCommand="SELECT ExhibitID, ExhibitType, ExhibitImage, InvestigatorID FROM Exhibits">
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:CMSSQL3ConnectionString1 %>"
DeleteCommand="DELETE FROM [Exhibits] WHERE [ExhibitID] = @ExhibitID AND [CaseID] = @CaseID"
InsertCommand="INSERT INTO [Exhibits] ([CaseID], [ExhibitType], [DateReceived], [StoredLocation], [OfficerID], [SuspectID], [InvestigatorID], [ManagerID], [AdminID]) VALUES (@CaseID, @ExhibitType, @DateReceived, @StoredLocation, @OfficerID, @SuspectID, @InvestigatorID, @ManagerID, @AdminID)"
ProviderName="<%$ ConnectionStrings:CMSSQL3ConnectionString1.ProviderName %>"
SelectCommand="SELECT UserID, UserName
FROM Users
WHERE UserID IN (SELECT UserID
FROM Users
WHERE UserType LIKE 'Investigator'
AND UserID NOT IN (SELECT InvestigatorID
From Exhibits
GROUP BY (InvestigatorID)
HAVING COUNT(InvestigatorID) > 2));"
UpdateCommand="UPDATE [Exhibits] SET [ExhibitType] = @ExhibitType, [DateReceived] = @DateReceived, [StoredLocation] = @StoredLocation, [OfficerID] = @OfficerID, [SuspectID] = @SuspectID, [InvestigatorID] = @InvestigatorID, [ManagerID] = @ManagerID, [AdminID] = @AdminID WHERE [ExhibitID] = @ExhibitID AND [CaseID] = @CaseID">
<DeleteParameters>
<asp:Parameter Name="ExhibitID" Type="Int32" />
<asp:Parameter Name="CaseID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="CaseID" Type="Int32" />
<asp:Parameter Name="ExhibitType" Type="String" />
<asp:Parameter Name="DateReceived" Type="DateTime" />
<asp:Parameter Name="StoredLocation" Type="String" />
<asp:Parameter Name="OfficerID" Type="String" />
<asp:Parameter Name="SuspectID" Type="Int32" />
<asp:Parameter Name="InvestigatorID" Type="String" />
<asp:Parameter Name="ManagerID" Type="String" />
<asp:Parameter Name="AdminID" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="ExhibitType" Type="String" />
<asp:Parameter Name="DateReceived" Type="DateTime" />
<asp:Parameter Name="StoredLocation" Type="String" />
<asp:Parameter Name="OfficerID" Type="String" />
<asp:Parameter Name="SuspectID" Type="Int32" />
<asp:Parameter Name="InvestigatorID" Type="String" />
<asp:Parameter Name="ManagerID" Type="String" />
<asp:Parameter Name="AdminID" Type="String" />
<asp:Parameter Name="ExhibitID" Type="Int32" />
<asp:Parameter Name="CaseID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
</td>
</tr>
</table>
</div>
<div>
<table class="style2">
<tr>
<td>
</td>
<td>
</td>
</tr>
</table>
</div>
</asp:Content>
最后是当前页面的.cs文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
namespace Prototype5
{
public partial class AllocateInvestigators : System.Web.UI.Page
{
SqlConnection caseConnection = new SqlConnection();
SqlConnection exhibitConnection = new SqlConnection();
DataSet caseDataSet = new DataSet();
DataSet exhibitDataSet = new DataSet();
SqlDataAdapter caseSqlDataAdapter = new SqlDataAdapter();
SqlDataAdapter exhibitSqlDataAdapter = new SqlDataAdapter();
protected void Page_Load(object sender, EventArgs e)
{
//exhibitGridView.Enabled = false;
}
protected void caseIDDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
exhibitGridView.Enabled = true;
string selectedCaseID = caseIDDropDownList.SelectedItem.Text.ToString();
CreateExhibitDataset();
DataView exhibitDataView = new DataView(exhibitDataSet.Tables[0]);
exhibitDataView.RowFilter = "CaseID = '" + selectedCaseID + "' ";
exhibitsDropDownList.DataSource = exhibitDataView;
exhibitsDropDownList.DataBind();
}
private void CreateDataSet()
{
caseConnection.ConnectionString =
distinctCaseIDSqlDataSource.ConnectionString;
caseSqlDataAdapter.SelectCommand = new
SqlCommand(distinctCaseIDSqlDataSource.SelectCommand, caseConnection);
caseSqlDataAdapter.Fill(caseDataSet);
}
private void CreateExhibitDataset()
{
exhibitConnection.ConnectionString =
exhibitIDSqlDataSource.ConnectionString;
exhibitSqlDataAdapter.SelectCommand = new
SqlCommand(exhibitIDSqlDataSource.SelectCommand, caseConnection);
exhibitSqlDataAdapter.Fill(exhibitDataSet);
}
}
}
依靠您的帮助... @ _ @ ...
这是你想要的?
我已经使用了示例NorthWind DB,因此您可能必须将DataSources添加到GridView。 这是标记
<asp:GridView ID='GridView1' runat="server" AutoGenerateColumns='False'
DataSourceID='ObjectDataSource1' DataKeyNames='ProductID'
AllowPaging='True'>
<Columns>
<asp:BoundField HeaderText='Name' DataField='ProductName' ReadOnly='true'/>
<asp:TemplateField HeaderText='Order'>
<EditItemTemplate>
<asp:DropDownList ID='DropDownList1' runat='server' DataTextField='ProductName' DataValueField='ProductID'>
<asp:ListItem Enabled='true' Selected='True' Value='2'>
</asp:ListItem>
<asp:ListItem Value='1'></asp:ListItem>
<asp:ListItem Value='3'></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID='Label1' runat="server" Text='<%# Eval("UnitsOnOrder") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton='true' ButtonType='Link' EditText='Edit' UpdateText='Update' CancelText='Cancel' />
</Columns>
</asp:GridView>
另外,您还必须设置DropDownList的数据源。 在上面的标记中,我对ListItems进行了硬编码。
单击更新链接后,GridView将触发OnRowUpdating事件...在事件处理程序中,您可能会获取控件的值(在该行中)并对其执行操作,例如更新数据。 下面是代码片段...(您必须在OnRowUpdating='HandleOnGridViewRowUpdating'
的标记中添加OnRowUpdating='HandleOnGridViewRowUpdating'
)
protected void HandleOnGridViewRowUpdating(Object sender, GridViewUpdateEventArgs e)
{
DropDownList dropDownList =
GridView1.Rows[e.RowIndex].FindControl("DropDownList1") as DropDownList;
//got the selected value of dropdown..
//get other required values from the GridView1...
//the DataKeyNames is set to ProductID.. so e.Keys["ProductID"] should give you the //ProductID..
//write a sql that takes these values and updates the db...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.