This is a migrated thread and some comments may be shown as answers.

Add new record to grid, with outside button.

3 Answers 482 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Bruno
Top achievements
Rank 1
Bruno asked on 14 Mar 2012, 08:00 PM
I wanna add a new record, and edit the records on grid, with button outside the grid. How could i do that?
Thats my asp code
<telerik:RadGrid ID="RadGrid2" runat="server" AllowMultiRowSelection="true" Width="300px"OnNeedDataSource="RadGrid2_NeedDataSource">
                <MasterTableView AutoGenerateColumns="False" DataKeyNames="codigo" EditMode="InPlace">
                    <Columns>
                        <telerik:GridTemplateColumn>
                            <ItemTemplate>
                                <asp:CheckBox runat="server" ID="CheckBox1" />
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridBoundColumn HeaderText="Código" DataField="codigo" UniqueName="codigo"
                            ReadOnly="True" Display="true" ColumnEditorID="GridTextBoxColumnEditor1" />
                        <telerik:GridBoundColumn HeaderText="Descrição" DataField="descricao" UniqueName="descricao"
                            ColumnEditorID="GridTextBoxColumnEditor1" />
                        <telerik:GridEditCommandColumn ButtonType="ImageButton">
                        </telerik:GridEditCommandColumn>
                    </Columns>
                </MasterTableView>
                <ClientSettings>
                    <ClientEvents OnRowDblClick="RowDblClick" />
                </ClientSettings>
            </telerik:RadGrid>
            <telerik:GridTextBoxColumnEditor ID="GridTextBoxColumnEditor1" runat="server" TextBoxStyle-Width="180px" />
        <asp:Button ID="btnDeletar" runat="server" Text="Deletar Items" OnClick="btnDeletar_Click" />
        <asp:Button ID="btnInserir" runat="server" Text="Inserir Items" OnClick="btnInserir_Click" />
        <asp:Button ID="btnEditar" runat="server" Text="Editar Items" OnClick="btnEditar_Click" />
        <telerik:RadWindowManager ID="Window" runat="server" EnableShadow="true" />
and thats my c# code
protected void RadGrid2_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    // Atribui dados a grid.
    RadGrid2.DataSource = this.GridSource;
    this.GridSource.PrimaryKey = new DataColumn[] { this.GridSource.Columns["codigo"] };
}
/// <summary>
/// Obtem a source do grid.
/// </summary>
private DataTable GridSource
{
    get
    {
        object obj = ViewState["GRID"];
        if ((!(obj == null)))
        {
            return ((DataTable)(obj));
        }
        DataTable myDataTable = new DataTable();
 
        myDataTable = Preenche();
        ViewState["GRID"] = myDataTable;
 
        return myDataTable;
    }
}
/// <summary>
/// Obtem data table com dados para grid.
/// </summary>
/// <returns>Retorna datatable</returns>
public DataTable Preenche()
{
    C001 c001 = new C001();
    DataTable dt = new DataTable();
 
    dt = c001.ObterDados();
    return dt;
}

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 15 Mar 2012, 06:38 AM
Hello Bruno,

I understand that you want to make the selected row editable on one button click and add a new record to the RadGrid on another.
Take a look into the following code.

Code to  make the grid in Edit mode:
C#:
protected void btnEditar_Click(object sender, EventArgs e)
{
 foreach (GridDataItem item in RadGrid2.SelectedItems)
 {
   item.Edit = true;
 }
 RadGrid2.Rebind();
}

Code to add new record:
C#:
protected void btnInserir_Click(object sender, EventArgs e)
{
 RadGrid2.MasterTableView.IsItemInserted = true;
 RadGrid2.MasterTableView.Rebind();
}

Hope this helps.

Thanks,
-Shinu.


0
Bruno
Top achievements
Rank 1
answered on 15 Mar 2012, 12:48 PM
Shinu, thankz for the reply, the edit button , works as i expected, but the Insert button let me explain better.
My user will click on Add New Record(the grid button) and will appear the textbox, but onItemCreated, i remove Insert button and Cancel, so i want when user wrote something on the textbox and click in outside Insert Button, these record be added on grid.

Thankz in advance.
0
Shinu
Top achievements
Rank 2
answered on 16 Mar 2012, 06:57 AM
Hello Bruno,

You can use the FireCommandEvent to forcefully fire thePerformInsertCommandName.
C#:
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
  if (e.CommandName == "PerformInsert")
  {//insert code here
  }
}
protected void btnInserir_Click(object sender, EventArgs e)
{
  RadGrid grid = (this.FindControl("RadGrid1") as RadGrid);
  grid.MasterTableView.InsertItem();
  (grid.MasterTableView.GetItems(GridItemType.CommandItem)[0] as GridCommandItem).FireCommandEvent(RadGrid.PerformInsertCommandName, string.Empty);
}

Thanks,
Shinu.
Tags
Grid
Asked by
Bruno
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Bruno
Top achievements
Rank 1
Share this question
or