RadGrid 中的复选框并将数据发送到数据库

发布于 2024-12-04 21:37:20 字数 749 浏览 2 评论 0原文

我想要 RADGRID 中的复选框。以下是详细要求:

我使用存储过程中的 AutoGenerateColumns="True" 填充所有 radgrid 列。我需要一个额外的复选框列。
我没有/需要任何字段来在网格加载期间绑定复选框。

用户可以选中任意数量的复选框,并且所选行的第二列数据应发送到数据库。

以下是我用来显示复选框的代码,但完整的复选框列将被禁用。

<MasterTableView CommandItemDisplay="None" HeaderStyle-BorderStyle="None">  
 <Columns>  
 <rad:GridCheckBoxColumn HeaderText="LinkRisk" AllowFiltering="false" ReadOnly="false" HeaderStyle-Width="3%">  
 </rad:GridCheckBoxColumn>  
 </Columns>  
 </MasterTableView>  

我需要帮助:
1. 获取复选框。
2.如何将数据发送到数据库。
3.如何将其保存到数据库中。

假设检查了 radgrid 的第 4,5,6 行。
我需要发送这些行中第二列的数据,即 44、55、66。
因此,在我的数据库中,表的 3 个新行应插入为:
ID值 44 55 a 66

提前致谢!

I want a checkbox in RADGRID. Following are detailed requirements:

All the radgrid columns i am populating with AutoGenerateColumns="True" from stored procedure. I need an extra checkbox column.
I don't have/need any field to bind checkbox during gridload.

User can check any number of checkboxes and the second column's data of selected rows should be sent to database.

Following is the code i have used to display checkbox, but the complete checkbox column is coming disabled.

<MasterTableView CommandItemDisplay="None" HeaderStyle-BorderStyle="None">  
 <Columns>  
 <rad:GridCheckBoxColumn HeaderText="LinkRisk" AllowFiltering="false" ReadOnly="false" HeaderStyle-Width="3%">  
 </rad:GridCheckBoxColumn>  
 </Columns>  
 </MasterTableView>  

I require help to :
1. Get the checkbox.
2. How to send data to database.
3. How to save it in database.

Supposed Row 4,5,6 of radgrid is checked .
I need to send data of second column in those rows i.e say 44, 55, 66.
So in my database 3 new rows of my table should be inserted as:
ID Value
a 44
a 55
a 66

Thanks in Advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉生梦死 2024-12-11 21:37:20

我继续遵循“Emaad Ali”的评论来帮助您找到解决方案。

第 1 步:

创建 Radgrid。 (我使用 Northwind 数据库作为数据源,因为它很简单,应该很容易理解。)在考虑“代码过载”之前,只需使用 Visual Studio 的向导连接到您的数据源,它将生成下面的大部分代码根据您想要的数据为您提供。

<telerik:RadGrid ID="RadGrid1" runat="server" CellSpacing="0" 
        DataSourceID="SqlDataSource1" GridLines="None">
        <MasterTableView AutoGenerateColumns="False" DataKeyNames="ProductID, ProductName"  
                        DataSourceID="SqlDataSource1">
        <CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>

        <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
        <HeaderStyle Width="20px"></HeaderStyle>
        </RowIndicatorColumn>

        <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column">
        <HeaderStyle Width="20px"></HeaderStyle>
        </ExpandCollapseColumn>

            <Columns>
                <telerik:GridBoundColumn DataField="ProductID" DataType="System.Int32" 
                    FilterControlAltText="Filter ProductID column" HeaderText="ProductID" 
                    ReadOnly="True" SortExpression="ProductID" UniqueName="ProductID">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="ProductName" 
                    FilterControlAltText="Filter ProductName column" HeaderText="ProductName" 
                    SortExpression="ProductName" UniqueName="ProductName">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="SupplierID" DataType="System.Int32" 
                    FilterControlAltText="Filter SupplierID column" HeaderText="SupplierID" 
                    SortExpression="SupplierID" UniqueName="SupplierID">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="UnitPrice" DataType="System.Decimal" 
                    FilterControlAltText="Filter UnitPrice column" HeaderText="UnitPrice" 
                    SortExpression="UnitPrice" UniqueName="UnitPrice">
                </telerik:GridBoundColumn>
                <telerik:GridTemplateColumn HeaderText="Update"> 
                <ItemTemplate> 
                    <asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBox2_CheckedChanged" /> 
                </ItemTemplate> 
            </telerik:GridTemplateColumn> 
            </Columns>

        <EditFormSettings>
        <EditColumn FilterControlAltText="Filter EditCommandColumn column"></EditColumn>
        </EditFormSettings>
        </MasterTableView>

        <FilterMenu EnableImageSprites="False"></FilterMenu>

        <HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default"></HeaderContextMenu>
    </telerik:RadGrid>

第 2 步:
在从数据源自动生成的列之后添加以下列。如果您查看步骤 1 中的代码,您会发现我已经添加了它。您可以逐字复制并粘贴下面的代码,它将起作用。

<telerik:GridTemplateColumn HeaderText="Update"> 
            <ItemTemplate> 
                <asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBox2_CheckedChanged" /> 
            </ItemTemplate> 
        </telerik:GridTemplateColumn> 

此时运行时,网格将如下所示:

A Walkthrough for your convienience
第 3 步:
在网格属性下的 DataKeyNames="" 字段中,您需要添加要从中读取数据的列的名称。你说你想要第二列的数据,所以我也使用了第二列。在 Radgrid 中,我的第二列有 DataField="ProductName",因此我需要将其添加到网格的 DataKeyNames 属性中,使其成为 DataKeyNames="ProductName"。这将使我们能够在步骤 4 中的 C# 代码中读取该列中的数据。

步骤 4:

转到后面的代码并粘贴此代码:

protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
{
    CheckBox chk = (CheckBox)sender;
    bool status = chk.Checked;
    GridDataItem item = (GridDataItem)chk.NamingContainer;
    string keyvalue = item.GetDataKeyValue("ProductName").ToString();
    string connectionString ="";
    if(status)
    {

    }
}

如您所见,此代码中引用了“ProductName”代码,因此您必须将其更改为您的列的名称。

第 5 步:

在 if(status) 条件中插入将数据发送到数据库所需的代码,以便它仅在您第一次检查时触发。

注意:

如果您需要从其他列获取数据,您只需将列名添加到 DataKeyNames="column1,column2" 字段,然后通过 c# 引用它们

string value1 = item.GetDataKeyValue("column1").ToString();
string value2 = item.GetDataKeyValue("column2").ToString();

即可 :将这些值连接到您可能使用的任何数据库代码中。

我不了解 Oracle,但我添加到 if(Status) 的 SQL 代码如下所示:

    SqlConnection SqlConnection = new SqlConnection(connectionString);
    SqlCommand SqlCommand = new SqlCommand();
    SqlCommand.CommandText = "update products set [UnitPrice] = '100' where [ProductName] = '" + keyvalue + "'";
    SqlCommand.Connection = SqlConnection;
    SqlConnection.Open();
    SqlCommand.ExecuteNonQuery();
    SqlConnection.Close();

这是一个基本更新,但您应该了解如何将其应用到代码中,将其插入到桌子。

I went ahead and followed "Emaad Ali's" comment to help you towards a solution.

Step 1:

Create your Radgrid. (I used the Northwind database as a datasource because it's simple and should be easy to follow along.) Before you think "code overload," just use visual studio's wizard to connect to your data source and it will generate a majority of the code below for you based on your desired data.

<telerik:RadGrid ID="RadGrid1" runat="server" CellSpacing="0" 
        DataSourceID="SqlDataSource1" GridLines="None">
        <MasterTableView AutoGenerateColumns="False" DataKeyNames="ProductID, ProductName"  
                        DataSourceID="SqlDataSource1">
        <CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>

        <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
        <HeaderStyle Width="20px"></HeaderStyle>
        </RowIndicatorColumn>

        <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column">
        <HeaderStyle Width="20px"></HeaderStyle>
        </ExpandCollapseColumn>

            <Columns>
                <telerik:GridBoundColumn DataField="ProductID" DataType="System.Int32" 
                    FilterControlAltText="Filter ProductID column" HeaderText="ProductID" 
                    ReadOnly="True" SortExpression="ProductID" UniqueName="ProductID">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="ProductName" 
                    FilterControlAltText="Filter ProductName column" HeaderText="ProductName" 
                    SortExpression="ProductName" UniqueName="ProductName">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="SupplierID" DataType="System.Int32" 
                    FilterControlAltText="Filter SupplierID column" HeaderText="SupplierID" 
                    SortExpression="SupplierID" UniqueName="SupplierID">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="UnitPrice" DataType="System.Decimal" 
                    FilterControlAltText="Filter UnitPrice column" HeaderText="UnitPrice" 
                    SortExpression="UnitPrice" UniqueName="UnitPrice">
                </telerik:GridBoundColumn>
                <telerik:GridTemplateColumn HeaderText="Update"> 
                <ItemTemplate> 
                    <asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBox2_CheckedChanged" /> 
                </ItemTemplate> 
            </telerik:GridTemplateColumn> 
            </Columns>

        <EditFormSettings>
        <EditColumn FilterControlAltText="Filter EditCommandColumn column"></EditColumn>
        </EditFormSettings>
        </MasterTableView>

        <FilterMenu EnableImageSprites="False"></FilterMenu>

        <HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default"></HeaderContextMenu>
    </telerik:RadGrid>

Step 2:
Add the following column after the auto-generated ones from your data source. If you look at the code from step 1, you will see I have already added it. You may copy and paste the code below in verbatim and it will work.

<telerik:GridTemplateColumn HeaderText="Update"> 
            <ItemTemplate> 
                <asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBox2_CheckedChanged" /> 
            </ItemTemplate> 
        </telerik:GridTemplateColumn> 

When run at this point the grid would look like this:

A Walkthrough for your convienience
Step 3:
In the field DataKeyNames="" under your grid properties, you need to add the name of the column for which you want to read the data from. You said you wanted data from the second column, so I too used the second column. In the Radgrid my second column has DataField="ProductName", so i need to add that to the DataKeyNames property of my grid, making it DataKeyNames="ProductName". This will allow us to read the data in that column in the c# code in step 4.

Step 4:

Go to the code behind and paste this code:

protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
{
    CheckBox chk = (CheckBox)sender;
    bool status = chk.Checked;
    GridDataItem item = (GridDataItem)chk.NamingContainer;
    string keyvalue = item.GetDataKeyValue("ProductName").ToString();
    string connectionString ="";
    if(status)
    {

    }
}

As you can see "ProductName" is referenced in this code, so you must change it to the name of your column.

Step 5:

Insert the code that would be needed to send the data to you DB inside the if(status) conditional so it only fires when you check it the first time.

Note:

If you need to get data from other columns, all you need to do is add the column names to the DataKeyNames="column1,column2" field and then reference them via the c#:

string value1 = item.GetDataKeyValue("column1").ToString();
string value2 = item.GetDataKeyValue("column2").ToString();

You may then concatenate these values into any DB code you may use.

I don't know about Oracle, but my SQL code that I added to the if(Status) looks like this:

    SqlConnection SqlConnection = new SqlConnection(connectionString);
    SqlCommand SqlCommand = new SqlCommand();
    SqlCommand.CommandText = "update products set [UnitPrice] = '100' where [ProductName] = '" + keyvalue + "'";
    SqlCommand.Connection = SqlConnection;
    SqlConnection.Open();
    SqlCommand.ExecuteNonQuery();
    SqlConnection.Close();

It's a basic update, but you should get the idea in regards to applying it to your code that would insert it into a table.

睡美人的小仙女 2024-12-11 21:37:20

使用模板列:

<tel:GridTemplateColumn>
   <ItemTemplate>
       <asp:Checkbox id="chk" runat="server" />
   </ItemTemplate>
</tel:GridTemplateColumn>

复选框列的问题是它只显示当前行进行编辑。它将为每一行显示一个复选框。

use a template column instead:

<tel:GridTemplateColumn>
   <ItemTemplate>
       <asp:Checkbox id="chk" runat="server" />
   </ItemTemplate>
</tel:GridTemplateColumn>

The issue with the checkbox column is that it only shows up for the current row to edit. It will show a checkbox for each row.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文