Hi,
I'd like to create a simple grid with one column not from a database.
And, I'd like to add rows to this grid each time a button is clicked, with the value of a combobox.
I can't do that, it doesnt works.
I'm a beginner, could you please tell me how to make that?
Thanks
I'd like to create a simple grid with one column not from a database.
And, I'd like to add rows to this grid each time a button is clicked, with the value of a combobox.
I can't do that, it doesnt works.
I'm a beginner, could you please tell me how to make that?
Thanks
7 Answers, 1 is accepted
0

Caro
Top achievements
Rank 1
answered on 05 Apr 2012, 11:09 AM
Nobody knows how to make that? :(
0

Shinu
Top achievements
Rank 2
answered on 05 Apr 2012, 12:45 PM
Hi Caro,
I guess you want to create rows on Button Click depending up on the value of the ComboBox. Here is a sample code snippet I tried. Please take a look into the following code.
ASPX:
C#:
Thanks,
Shinu.
I guess you want to create rows on Button Click depending up on the value of the ComboBox. Here is a sample code snippet I tried. Please take a look into the following code.
ASPX:
<
telerik:RadGrid
ID
=
"RadGrid1"
runat
=
"server"
onneeddatasource
=
"RadGrid1_NeedDataSource"
>
<
MasterTableView
>
<
Columns
>
<
telerik:GridTemplateColumn
>
<
ItemTemplate
>
<
asp:Label
ID
=
"Label1"
runat
=
"server"
></
asp:Label
>
</
ItemTemplate
>
</
telerik:GridTemplateColumn
>
</
Columns
>
</
MasterTableView
>
</
telerik:RadGrid
>
<
telerik:RadComboBox
ID
=
"RadComboBox1"
runat
=
"server"
>
<
Items
>
<
telerik:RadComboBoxItem
Text
=
"1"
Value
=
"1"
/>
<
telerik:RadComboBoxItem
Text
=
"2"
Value
=
"2"
/>
<
telerik:RadComboBoxItem
Text
=
"3"
Value
=
"3"
/>
<
telerik:RadComboBoxItem
Text
=
"4"
Value
=
"4"
/>
</
Items
>
</
telerik:RadComboBox
>
<
asp:Button
ID
=
"Button1"
runat
=
"server"
onclick
=
"Button1_Click"
/>
C#:
public
static
int
num = 0;
protected
void
Button1_Click(
object
sender, EventArgs e)
{
num = Convert.ToInt32(RadComboBox1.SelectedValue);
RadGrid1.Rebind();
}
protected
void
RadGrid1_NeedDataSource(
object
sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
DataTable ds =
new
DataTable();
RadGrid1.DataSource = addBlankLines(ds, num);
}
private
DataTable addBlankLines(DataTable ds,
int
num)
{
if
(num != 0)
{
DataRow drBlank =
default
(DataRow);
for
(
int
i = 0; i < num; i++)
{
drBlank = ds.NewRow();
ds.Rows.Add(drBlank);
}
}
return
ds;
}
Thanks,
Shinu.
0

Caro
Top achievements
Rank 1
answered on 05 Apr 2012, 01:52 PM
Hi Shinu,
Thanks for your answer.
Your code had a few problems : The lines are added two by two on the grid and I don't know how to fill the blanks lines with my combobox selected values.
Caro
Thanks for your answer.
Your code had a few problems : The lines are added two by two on the grid and I don't know how to fill the blanks lines with my combobox selected values.
Caro
0

Caro
Top achievements
Rank 1
answered on 06 Apr 2012, 07:22 AM
Up please :)
0

Caro
Top achievements
Rank 1
answered on 06 Apr 2012, 08:09 AM
So ... I've find ...
ASP.NET :
C# :
It works but each time I push the button
I want that each time I push the button, it add to the grid the new value but keep the old too ....
Could you help me to do that?
ASP.NET :
<
telerik:RadComboBox
ID
=
"MT"
Runat
=
"server"
DataSourceID
=
"MT_get"
DataTextField
=
"name"
DataValueField
=
"id"
>
</
telerik:RadComboBox
>
<telerik:RadButton
ID
=
"MTbutton"
runat
=
"server"
Text
=
"Add"
CausesValidation
=
"true"
onclick
=
"MTbutton_Click"
>
</
telerik:RadButton
>
<
telerik:RadGrid
ID
=
"gridMT"
runat
=
"server"
Visible
=
"false"
>
</
telerik:RadGrid
>
C# :
protected void MTbutton_Click(object sender, EventArgs e)
{
gridMT.Visible = true;
DataTable ds = new DataTable();
DataColumn colItem = new DataColumn("Name", Type.GetType("System.String"));
ds.Columns.Add(colItem);
DataRow NewRow;
NewRow = ds.NewRow();
NewRow["Name"] = MT.SelectedItem.Text;
ds.Rows.Add(NewRow);
gridMT.DataSource = ds;
gridMT.DataBind();
}
It works but each time I push the button
"MTbutton
" the old value is drop and we can see only the new value.I want that each time I push the button, it add to the grid the new value but keep the old too ....
Could you help me to do that?
0

Shinu
Top achievements
Rank 2
answered on 09 Apr 2012, 10:43 AM
Hi Caro,
Please check the following code snippet.
ASPX:
C#:
Thanks,
-Shinu.
Please check the following code snippet.
ASPX:
<
asp:ScriptManager
ID
=
"scrpt"
runat
=
"server"
>
</
asp:ScriptManager
>
<
telerik:RadComboBox
ID
=
"MT"
runat
=
"server"
DataSourceID
=
"SqlDataSource1"
DataTextField
=
"EmployeeID"
DataValueField
=
"EmployeeID"
>
</
telerik:RadComboBox
>
<
telerik:RadButton
ID
=
"MTbutton"
runat
=
"server"
Text
=
"Add"
CausesValidation
=
"true"
OnClick
=
"MTbutton_Click"
>
</
telerik:RadButton
>
<
telerik:RadGrid
ID
=
"gridMT"
runat
=
"server"
Visible
=
"false"
onneeddatasource
=
"gridMT_NeedDataSource"
>
</
telerik:RadGrid
>
C#:
public
static
DataTable ds=
new
DataTable();
protected
void
Page_Load(
object
sender, EventArgs e)
{
if
(!IsPostBack)
{
Session[
"Table"
] =
null
;
DataColumn colItem =
new
DataColumn(
"Name"
, Type.GetType(
"System.String"
));
ds.Columns.Add(colItem);
}
}
protected
void
MTbutton_Click(
object
sender, EventArgs e)
{
gridMT.Rebind();
}
protected
void
gridMT_NeedDataSource(
object
sender, GridNeedDataSourceEventArgs e)
{
gridMT.Visible =
true
;
DataRow NewRow;
NewRow = ds.NewRow();
NewRow[
"Name"
] = MT.SelectedItem.Text;
ds.Rows.Add(NewRow);
if
(Session[
"Table"
] !=
null
)
{
ds = (DataTable)Session[
"Table"
];
}
gridMT.DataSource = ds;
//populate RadGrid with datatable
Session[
"Table"
] = ds;
}
Thanks,
-Shinu.
0

Caro
Top achievements
Rank 1
answered on 10 Apr 2012, 08:02 AM
Hi Shinu
It works, thank you very much !!
It works, thank you very much !!