更新网格视图中的特定列

时间:2010-07-29 14:38:23

标签: c#

我有一个从数据库中检索数据的GridView。用户有角色,一些用户将更新GridView中的一列,其他用户将根据角色更新其他列,请参阅下面的代码!我创建了如下的存储过程,并添加了代码。当我测试项目时,在网格视图中编辑列之后以角色(AR-Tr​​anslator)为例,它用NULL值替换其他的fieds,那么请问我能做什么?

存储过程

ALTER Proc [dbo].[GetAllTranslation]
 AS
BEGIN 
  Select Id,Word 
   ,Trans_AR
   ,Trans_EN
   ,Add_Date 
  From Translation 
END

ALTER Proc [dbo].[UpdateTranslation]
(
  @IdINT
  ,@Word NVARCHAR(MAX)
  ,@Trans_AR NVARCHAR(MAX)
  ,@Trans_EN NVARCHAR(MAX)


)
AS
BEGIN 
  UPDATE  Translation SET

  Word =@Word 
  ,Trans_AR=@Trans_AR 
  ,Trans_EN=@Trans_EN 

  WHERE Id=@Id
END

aspx页面

 <div class="m10">
  <asp:GridView ID="GridView1" runat="server" BackColor="White" 
    BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" 
    ForeColor="Black" GridLines="Vertical" AllowPaging="True" 
    AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="Id" 
    DataSourceID="SDSTrans" onrowupdated="GridView1_RowUpdated">
    <FooterStyle BackColor="#CCCC99" />
    <RowStyle BackColor="#F7F7DE" />
    <Columns>
        <asp:CommandField HeaderText="Function" ShowEditButton="True" />
        <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" 
            ReadOnly="True" SortExpression="Id" />
        <asp:BoundField DataField="Word" HeaderText="Word" SortExpression="Word" />
        <asp:BoundField DataField="Trans_AR" HeaderText="Trans_AR" 
            SortExpression="Trans_AR" />
        <asp:BoundField DataField="Trans_EN" HeaderText="Trans_EN" 
            SortExpression="Trans_EN" />
        <asp:BoundField DataField="Add_Date" HeaderText="Add_Date" 
            SortExpression="Add_Date" />
    </Columns>
    <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
    <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
    <AlternatingRowStyle BackColor="White" />
</asp:GridView>
    <asp:SqlDataSource ID="SDSTrans" runat="server" 
        ConnectionString="Data Source=ELARABY-1EACFA3\SQLEXPRESS;Initial Catalog=ElarabyGroup;Integrated Security=True" 
        ProviderName="System.Data.SqlClient" SelectCommand="GetAllTranslation" 
        SelectCommandType="StoredProcedure" UpdateCommand="UpdateTranslation" 
        UpdateCommandType="StoredProcedure">
        <UpdateParameters>
            <asp:ControlParameter ControlID="GridView1" Name="Id" 
                PropertyName="SelectedValue" Type="Int32" />
            <asp:ControlParameter ControlID="GridView1" Name="Word" 
                PropertyName="SelectedValue" Type="String" />
            <asp:ControlParameter ControlID="GridView1" Name="Trans_AR" 
                PropertyName="SelectedValue" Type="String" />
            <asp:ControlParameter ControlID="GridView1" Name="Trans_EN" 
                PropertyName="SelectedValue" Type="String" />
        </UpdateParameters>
    </asp:SqlDataSource>
<asp:Label ID="ResultLBL" runat="server" Visible="false"></asp:Label>
</div>

CS

public void CheckLoginAuthorty()
{
    using (SqlConnection Con = Connection.GetConnection())
    {
        SqlCommand com = new SqlCommand("CheackLoginInRole", Con);
        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add(Parameter.NewNVarChar("@Login", Session.Contents["Username"].ToString()));
        object O = com.ExecuteScalar();

        if (O != null)
        {
            string S = O.ToString();

            if (IsInRole("AR-Translator", O.ToString()))
            {

                ((BoundField)GridView1.Columns[2]).ReadOnly = false;
                ((BoundField)GridView1.Columns[2]).InsertVisible = false;
                ((BoundField)GridView1.Columns[4]).ReadOnly = false;
                ((BoundField)GridView1.Columns[4]).InsertVisible = false;


            }

            else if (IsInRole("EN-Translator", O.ToString()))
            {
                ((BoundField)GridView1.Columns[2]).ReadOnly = true;
                ((BoundField)GridView1.Columns[2]).InsertVisible = false;
                ((BoundField)GridView1.Columns[3]).ReadOnly = true;
                ((BoundField)GridView1.Columns[3]).InsertVisible = false;
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

我还没有完全理解,但如果我确实有这个要点...默认从数据库返回的空值如下:

ALTER Proc [dbo].[GetAllTranslation]
 AS
BEGIN 
  Select Id
   ,isnull(Word,'') as 'Word'
   ,isnull(Trans_AR,'') as 'Trans_AR'
   ,isnull(Trans_EN,'') as 'Trans_EN'
   ,Add_Date
  From Translation 
END

答案 1 :(得分:0)

我猜你需要像这样更新你的“更新”SP

ALTER Proc [dbo].[UpdateTranslation]
(
  @IdINT
  ,@Word NVARCHAR(MAX)
  ,@Trans_AR NVARCHAR(MAX)
  ,@Trans_EN NVARCHAR(MAX)


)
AS
BEGIN 
  UPDATE  Translation SET

  Word =isnull(@Word,Word)
  ,Trans_AR=isnull(@Trans_AR, Trans_AR)
  ,Trans_EN=isnull(@Trans_EN ,Trans_EN)

  WHERE Id=@Id
END