Create Buttons to Insert Values in rad grid

8 posts, 0 answers
  1. Mohamed El-Sayed
    Mohamed El-Sayed avatar
    33 posts
    Member since:
    Apr 2009

    Posted 08 May 2014 Link to this post

    Dears ,

    I have a rad grid contain data and instead use edit to but value in one cell for every row , I want to create a two buttons in every row to put this value
    Ex:

    the cell in database name " mgm_con" I want when I press in the button to put in this cell " confirmed " or the second button " n't confirmed "
  2. Mohamed El-Sayed
    Mohamed El-Sayed avatar
    33 posts
    Member since:
    Apr 2009

    Posted 08 May 2014 in reply to Mohamed El-Sayed Link to this post

    and if there is availability to put also a text box name in Db " Reason" to add text in it and after press in any of the two button make this row disappeared and n't to show in the grid
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 09 May 2014 in reply to Mohamed El-Sayed Link to this post

    Hi Beka,

    I am not sure about the requirements. Following my assumptions about the requirement. Please have a look on the following steps.
    1. Update the Database value 'mgm_con' onClick of the RadButton and also reflect the updation in RadGrid.
    2. Hide a particular row by checking the value in the TextBox being added into another column.

    ASPX:
    <telerik:RadGrid ID="radgrdOrders" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="False" CellSpacing="-1" GridLines="Both">
        <MasterTableView DataKeyNames="CountryId">
            <Columns>
                <telerik:GridBoundColumn DataField="CountryName" UniqueName="CountryName">
                    <ColumnValidationSettings>
                        <ModelErrorMessage Text=""></ModelErrorMessage>
                    </ColumnValidationSettings>
                </telerik:GridBoundColumn>
                <telerik:GridTemplateColumn>
                    <ItemTemplate>
                        <telerik:RadButton ID="radbtnConfirm" runat="server" Text="ConfirmButton" OnClick="radbtnConfirm_Click">
                        </telerik:RadButton>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridTemplateColumn>
                    <ItemTemplate>
                        <telerik:RadButton ID="radbtnNotConfirm" runat="server" Text="NotConfirmButton" OnClick="radbtnNotConfirm_Click">
                        </telerik:RadButton>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridTemplateColumn>
                    <ItemTemplate>
                        <telerik:RadTextBox ID="radTxtReason" runat="server">
                        </telerik:RadTextBox>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>

    C#:
    protected void UpdateGrid(GridDataItem item, string datakey, string updateText)
    {
        string connectionstring = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection sqlconn = new SqlConnection(connectionstring);
        RadTextBox textBox = (RadTextBox)item.FindControl("radTxtReason");
        if (textBox.Text != string.Empty)
        {
            item.Visible = false;
        }
        else
        {
            sqlconn.Open();
            SqlCommand updateCommand = new SqlCommand("UPDATE Country SET CountryName=@CountryName WHERE CountryId=@UpdateValue", sqlconn);
            updateCommand.Parameters.AddWithValue("@UpdateValue", datakey);
            updateCommand.Parameters.AddWithValue("@CountryName", updateText);
            updateCommand.ExecuteScalar();
            sqlconn.Close();
            radgrdOrders.Rebind();
        }
    }
    protected void radbtnConfirm_Click(object sender, EventArgs e)
    {
        RadButton Button = (RadButton)sender;
        GridDataItem item = (GridDataItem)Button.NamingContainer;
        string datakey = item.GetDataKeyValue("CountryId").ToString();
        string updateText = "Confimed";
        UpdateGrid(item,datakey,updateText);
    }
    protected void radbtnNotConfirm_Click(object sender, EventArgs e)
    {
        RadButton Button = (RadButton)sender;
        GridDataItem item = (GridDataItem)Button.NamingContainer;
        string datakey = item.GetDataKeyValue("CountryId").ToString();
        string updateText = "NotConfimed";
        UpdateGrid(item, datakey, updateText);
    }

    Let me know if you have any concern.   
    Thanks,
    Shinu.
  5. Mohamed El-Sayed
    Mohamed El-Sayed avatar
    33 posts
    Member since:
    Apr 2009

    Posted 09 May 2014 in reply to Shinu Link to this post

    Sorry Shinu I tried but it didn't work a lot of errors
    look may this info help :

    Ex :

    DB :
    one table " Home "
    H_ID
    H_Adress
    H_status

    I want to use the grid to get those data and I want to use the buttons in the H_status  , confirm or n't confirm

    could you try to check by this way
  6. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 11 May 2014 in reply to Mohamed El-Sayed Link to this post

    Hi Beka,

    Please have a look into0 the sample code snippet which works fine at my end.

    ASPX:
    <telerik:RadGrid ID="radgrdOrders" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="False" CellSpacing="-1" GridLines="Both">
        <MasterTableView DataKeyNames="H_ID">
            <Columns>
                <telerik:GridBoundColumn DataField="H_status" UniqueName="H_status">
                </telerik:GridBoundColumn>
                <telerik:GridTemplateColumn>
                    <ItemTemplate>
                        <telerik:RadButton ID="radbtnConfirm" runat="server" Text="ConfirmButton" OnClick="radbtnConfirm_Click">
                        </telerik:RadButton>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridTemplateColumn>
                    <ItemTemplate>
                        <telerik:RadButton ID="radbtnNotConfirm" runat="server" Text="NotConfirmButton" OnClick="radbtnNotConfirm_Click">
                        </telerik:RadButton>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>

    C#:
    protected void radbtnConfirm_Click(object sender, EventArgs e)
    {
        RadButton Button = (RadButton)sender;
        GridDataItem item = (GridDataItem)Button.NamingContainer;
        string datakey = item.GetDataKeyValue("H_ID").ToString();
        string updateText = "Confimed";
        UpdateGrid(item,datakey,updateText);
    }
    protected void radbtnNotConfirm_Click(object sender, EventArgs e)
    {
        RadButton Button = (RadButton)sender;
        GridDataItem item = (GridDataItem)Button.NamingContainer;
        string datakey = item.GetDataKeyValue("H_ID").ToString();
        string updateText = "NotConfimed";
        UpdateGrid(item, datakey, updateText);
    }
    protected void UpdateGrid(GridDataItem item, string datakey, string updateText)
    {
        string connectionstring = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection sqlconn = new SqlConnection(connectionstring);
        sqlconn.Open();
        SqlCommand updateCommand = new SqlCommand("UPDATE Home SET H_status=@H_status WHERE H_ID=@UpdateValue", sqlconn);
        updateCommand.Parameters.AddWithValue("@UpdateValue", datakey);
        updateCommand.Parameters.AddWithValue("@H_status", updateText);
        updateCommand.ExecuteScalar();
        sqlconn.Close();
        radgrdOrders.Rebind();
    }

    Please provide your full code if it doesn't help.
    Thanks,
    Shinu.
  7. Mohamed El-Sayed
    Mohamed El-Sayed avatar
    33 posts
    Member since:
    Apr 2009

    Posted 14 May 2014 in reply to Shinu Link to this post

    Thanks SHINU it worked
  8. Mohamed El-Sayed
    Mohamed El-Sayed avatar
    33 posts
    Member since:
    Apr 2009

    Posted 14 May 2014 in reply to Shinu Link to this post

    thanks SHINU it worked very well , I just also need to retrieve the data when I press for ex: when I press on confirm I want to get the data of this row 
    H_ID
    H_Adress
    H_status
    using this code to send it in mail and add the details in the body of the mail
    SmtpClient client = new SmtpClient();
                    client.DeliveryMethod = SmtpDeliveryMethod.Network;
                    client.EnableSsl = true;
                    client.Host = "smtp.gmail.com";
                    client.Port = 587;
     
                    // setup Smtp authentication
                    System.Net.NetworkCredential credentials =
                        new System.Net.NetworkCredential("xxxx@yyy", "xxxx");
                    client.UseDefaultCredentials = false;
                    client.Credentials = credentials;
     
                    MailMessage msg = new MailMessage();
                    msg.From = new MailAddress("xxxx@yyy");
                    msg.To.Add("mm@zzz");
     
                    msg.Subject = (" new ");
                    msg.IsBodyHtml = true;
                    msg.Body = ("");
                    try
                    {
                        client.Send(msg);
     
                    }
                    catch (Exception ex)
                    {
                    }
  9. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 14 May 2014 in reply to Mohamed El-Sayed Link to this post

    Hi Beka,

    You can try the following code snippet in the confirm button click to access the row values.

    C#:
    protected void radbtnConfirm_Click(object sender, EventArgs e)
    {
      RadButton rbtnConfirm= (RadButton)sender;
      GridDataItem dataItem = (GridDataItem)rbtnConfirm.NamingContainer;
      string H_ID= dataItem .GetDataKeyValue("H_ID").ToString(); //Access dataKey value
      string H_Adress = dataItem["H_Adress "].Text;// Access a BoundColumn value
      // Your code
     }

    Thanks,
    Shinu
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017