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

Add Columns by Button Press

10 Answers 364 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Simon
Top achievements
Rank 1
Simon asked on 06 Mar 2014, 07:57 AM
Hi,I need some help with adding column dynamicly by pressing button,
Example:
<asp:textbox runat="server" ID="txtName" />
 <asp:Button runat="server" ID="btnAddColumn" Text="Add Column" />
 <telerik:RadGrid runat="server" ID="gridData" AutoGenerateColumns="false" ShowHeader="true"   Width="100%" Height="500px" ShowStatusBar="true" >
        <MasterTableView  EditMode="InPlace" 
ShowHeader="true" ShowFooter ="true" ShowHeadersWhenNoRecords="true"  >
..........
 </MasterTableView>
    </telerik:RadGrid>

on server side ,on button click event i tryingh to add  GridBoundColumn with name from txtName
and HeaderText from txtName and DataField from txtName,I also updating my datasource with new column
by nothing,the Column is added (i testing it with additional postback button pressing) but there is no visible Header text on client side and the header text on server side is empty too,

    Dim ContainerColumn As New GridBoundColumn
                gridData.MasterTableView.Columns.Add(ContainerColumn)
                AddColumnToData(txtName.Text)
                ContainerColumn.Display = True
                ContainerColumn.HeaderText = txtName.Text
                ContainerColumn.DataField = txtName.Text
                ContainerColumn.UniqueName = ContainerColumn.DataField






10 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 06 Mar 2014, 09:12 AM
Hi Simon,

Please try the following code snippet in the button click to generate the columns.

C#:
protected void Button1_Click(object sender, EventArgs e)
{
  string value = TextBox1.Text;
  GridBoundColumn boundColumn;
  boundColumn = new GridBoundColumn();
  RadGrid1.MasterTableView.Columns.Add(boundColumn);
  boundColumn.DataField = value;
  boundColumn.HeaderText = value;
  boundColumn.UniqueName = value;  
  RadGrid1.Rebind();
}

Thanks,
Shinu
0
Simon
Top achievements
Rank 1
answered on 06 Mar 2014, 09:43 AM
Thanks for the fast response,Additional problem,
when i adding columns in this scenario:
1)Add break with value 5
2)Add break with value 7
3)Add Container with some data from dropdown 
i got an error:Invalid column group configuration! Column "containers" cannot be after column "CONTAINER_RATEBLBULK"
Line 76: gridData.Rebind()
How can i send you sources?







0
Simon
Top achievements
Rank 1
answered on 06 Mar 2014, 09:46 AM
The sources
Just rename the file with rar extention
0
Simon
Top achievements
Rank 1
answered on 06 Mar 2014, 09:49 AM
Another thing 'i see after adding 2 columns ,grid have 2 adding some empty columns on client?
0
Shinu
Top achievements
Rank 2
answered on 07 Mar 2014, 05:53 AM
Hi Simon,

I'm not clear about your issues. Please elaborate with screenshots. Paste your code snippet here as well.
You can take a look at the following article on Creating a RadGrid Programmatically which may help you to create columns.

Thanks,
Shinu
0
Simon
Top achievements
Rank 1
answered on 10 Mar 2014, 12:31 PM
Hi,
Actually I'm looking for some example to add/remove Columns with RadGrid programmaticly.
It will be Form with textbox,and button outside the grid.After clicking the button it must add Column to DataTable and to RagGrid
The additional thisg is what grid must have some Static columns .
Can you provide some examples
0
Shinu
Top achievements
Rank 2
answered on 11 Mar 2014, 04:19 AM
Hi Simon,

Please take a look at the sample code snippet. Provide your code snippet for more help.

ASPX:
<asp:TextBox ID="colTxtBox" runat="server"></asp:TextBox>
<asp:Button ID="ColCreateBtn" runat="server" Text="Create Column" OnClick="ColCreateBtn_Click" />
<asp:Button ID="ColRemoveBtn" runat="server" Text="Remove Column" OnClick="ColRemoveBtn_Click" />
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" DataSourceID="SqlDataSource1" AllowPaging="true" AllowSorting="true">
    <MasterTableView DataKeyNames="OrderID">
        <Columns>
            <telerik:GridBoundColumn UniqueName="OrderID" DataField="OrderID" HeaderText="OrderID" />
            <telerik:GridBoundColumn DataField="ShipCity" HeaderText="ShipCity" UniqueName="ShipCity" />
        </Columns>
    </MasterTableView>
</telerik:RadGrid>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Northwind_newConnectionString3 %>"
    SelectCommand="SELECT  * FROM [Orders]"></asp:SqlDataSource>

C#:
protected void ColCreateBtn_Click(object sender, EventArgs e)
{
    string value = colTxtBox.Text;
    GridBoundColumn boundColumn;
    boundColumn = new GridBoundColumn();
    RadGrid1.MasterTableView.Columns.Add(boundColumn);
    boundColumn.DataField = value;
    boundColumn.HeaderText = value;
    boundColumn.UniqueName = value;
    RadGrid1.Rebind();
}
protected void ColRemoveBtn_Click(object sender, EventArgs e)
{
    string value = colTxtBox.Text;
    RadGrid1.MasterTableView.GetColumn(value).Visible = false;     
}

Thanks,
Shinu
0
Kostadin
Telerik team
answered on 11 Mar 2014, 08:02 AM
Hi Simon,

I would recommend you to examine the following help article which describes how to change the grid structure on postback.

Regards,
Kostadin
Telerik

DevCraft Q1'14 is here! Watch the online conference to see how this release solves your top-5 .NET challenges. Watch on demand now.

0
Simon
Top achievements
Rank 1
answered on 11 Mar 2014, 01:01 PM
Hi ,
Thanks for help.
The only thing i cant understand is how to access template item controls that was created programmaticly 
for example:
I have created item tepmplate with RADTEXTBOX,and put it to culumns
Dim  tc = New GridTemplateColumn
 tc.ItemTemplate = New MyTemplateAmount(c.ColumnName)
 gridData.MasterTableView.Columns.Add(tc)
 tc.HeaderText = "MINIMUM"
 tc.DataField = c.ColumnName
 tc.UniqueName = c.ColumnName
the name of RadTextbox inside the MyTemplateAmount is txt+c.ColumnName

On client side i changing the data inside RADTEXTBOX,next step i press button(postback) ,run on items:
For Each item As GridDataItem In gridData.MasterTableView.Items
but I cant access RADTEXTBOX,it doesnt exists in Items:
Dim text As RadTextBox = item(c.UniqueName).FindControl("txtMINIMUM")


Please help


0
Shinu
Top achievements
Rank 2
answered on 13 Mar 2014, 11:52 AM
Hi Simon,

You are not able to get the RadTextBox value because its not being saved to view state when created on the button click. Column templates must be added in the Page_Init event handler, so that the template controls can be added to the ViewState. You may try a similar approach to create template column and access values on button click.

ASPX:
<asp:Button ID="Button2" runat="server" Text="Create Column" OnClick="Button2_Click" OnClientClick="SetSource(this.id)" />
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
<asp:Button ID="Button1" runat="server" Text="Get Values" OnClick="Button1_Click" />
<asp:HiddenField ID="CustomHiddenField" runat="server" />

C#:
Private grid As RadGrid
Protected Sub Page_Init(sender As Object, e As EventArgs)
    grid = New RadGrid()
    grid.AutoGenerateColumns = False
    grid.DataSourceID = "SqlDataSource1"
    grid.AllowPaging = True
    grid.PageSize = 10
    grid.Skin = "Outlook"
    Dim templateColumn As New GridTemplateColumn()
    If IsPostBack Then
        Dim ControlID As String = String.Empty
        If Not [String].IsNullOrEmpty(Request.Form(CustomHiddenField.UniqueID)) Then
            ControlID = Request.Form(CustomHiddenField.UniqueID)
            Dim ColumnName As String = "ShipCity"
            templateColumn.ItemTemplate = New MyTemplate(ColumnName)
            templateColumn.HeaderText = ColumnName
            templateColumn.UniqueName = ColumnName
            templateColumn.DataField = ColumnName
        End If
    End If
    grid.MasterTableView.Columns.Add(templateColumn)
    PlaceHolder1.Controls.Add(grid)
End Sub
Private Class MyTemplate
    Implements ITemplate
 
    Protected textBox As RadTextBox
    Private colname As String
    Public Sub New(cName As String)
        colname = cName
    End Sub
    Public Sub InstantiateIn(container As System.Web.UI.Control)
        textBox = New RadTextBox()
        textBox.ID = Convert.ToString("txt") & colname
        container.Controls.Add(textBox)
    End Sub
End Class
Protected Sub Button1_Click(sender As Object, e As EventArgs)
    For Each items As GridDataItem In grid.MasterTableView.Items
        Dim txt As RadTextBox = DirectCast(items.FindControl("txtShipCity"), RadTextBox)
    Next
End Sub
Protected Sub Button2_Click(sender As Object, e As EventArgs)
    grid.Rebind()
End Sub

JS:
<script type="text/javascript">
 function SetSource(SourceID) {
     var hidSourceID =
     document.getElementById("<%=CustomHiddenField.ClientID%>");
     hidSourceID.value = SourceID;
   }
</script>

Thanks,
Shinu
Tags
Grid
Asked by
Simon
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Simon
Top achievements
Rank 1
Kostadin
Telerik team
Share this question
or