开发者

CheckBox in RadGrid and Sending data to Database

开发者 https://www.devze.com 2023-04-05 06:45 出处:网络
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 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 checkboxe开发者_开发知识库s 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!


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:

CheckBox in RadGrid and Sending data to Database

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号