复选框radgrid控件和发送数据到数据库 [英] CheckBox in RadGrid and Sending data to Database

查看:294
本文介绍了复选框radgrid控件和发送数据到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在radgrid控件的复选框。以下是详细的要求:

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

所有radgrid控件列我与从的AutoGenerateColumns存储过程填充=真。我需要一个额外的复选框列。结果
我没有/需要任何领域gridload中绑定复选框。

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.

以下是code我已经用于显示复选框,但完整的复选框列即将关闭。

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>  

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

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

radgrid控件的咋办4,5,6行检查。结果
我需要在这些行即说44,55,66 - 发送第二列的数据
所以,在我的数据库我的表3新行应插入为:结果
ID值
44
55
66

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

先谢谢了!

推荐答案

我继续遵循Emaad阿里的评论,以帮助你走向一个解决方案。

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

第1步:

创建radgrid控件。 (我用Northwind数据库作为数据源,因为它的简单,应该很容易跟随。)在你觉得code超载,只是使用Visual Studio的向导连接到数据源,它会产生一个多数低于code为根据您所需的数据。

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>

第二步:
从数据源自动生成的那些后添加以下列。如果你看一下步骤1中的code,你会看到我已经添加它。您可以复制和逐字中粘贴如下code,它会工作。

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> 

在此时运行Grid是这样的:

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


第3步:
在现场的DataKeyNames =在你的网格属性,您需要添加要读取的数据列的名称。你说你从第二列所需的数据,所以我也用的第二列。在radgrid控件我的第二列的DataField =产品名称,所以我需要将它添加到我的网格的DataKeyNames属性,使得它的DataKeyNames =产品名称。这将使我们能够在C#code列第4步中读取数据。

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.

第四步:

转至code的后面并粘贴此code:

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)
    {

    }
}

正如你所看到的产品名称,在此code引用,所以你必须将其更改为你列的名称。

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

第五步:

将code这将需要将数据发送到你的数据库,如果(状态)的内部条件,因此只有火灾时,你选择​​了它的第一次。

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.

注意:

如果您需要从其他列的数据,所有你需要做的就是添加的列名到的DataKeyNames =列1,列2字段,然后通过C#中引用它们:

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();

您可以再串联这些值到任何数据库code你可以使用。

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

我不知道甲骨文,但我的SQL code,我加入到如果(状态)是这样的:

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();

这是一个基本的更新,但你应该得到的想法在问候它应用到你的code,将它插入表中。

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.

这篇关于复选框radgrid控件和发送数据到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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