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

Access Dynamic Grid Column and rows

9 Answers 162 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kshitiz
Top achievements
Rank 1
Kshitiz asked on 11 May 2012, 06:39 PM
I have created fully dynmic grid with item template as total all through programming code .
Now am facing a difficutly .. i have put a Update buton since i have textboxes where user can change input in dynamic grid
I TRY To update on onclickevent
but am not able to access grid at all 
grid.columns.count is alwazz null ..
i need to loop to get the dynamic ids ... but am not able to access to elements in dynamic grid .


protected void lnkUpdateAll_Click(object sender, EventArgs e)
        {
    for (int OuterCol = 1; OuterCol < gridCntrl.Columns.Count; OuterCol++) -->getting zero :( 
            {
                GridTemplateColumn templateColumn = new GridTemplateColumn();
              string templateColumnName = ds.Tables[0].Columns[OuterCol].ColumnName;

                for (int inner = 0; inner < gridCntrl.Items.Count; inner++)
                {
                    double total;
                    string id;
                    if (true)
                    {
                id = (gridCntrl.Items[OuterCol].FindControl(templateColumnName) as RadNumericTextBox).Text.ToString();
                        total = double.Parse((gridCntrl.Items[OuterCol].FindControl(templateColumnName)as RadNumericTextBox).Value.ToString()); ;

}
                      

                    }
 

9 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 13 May 2012, 08:30 AM
Hello Kshitiz,

Please make sure, You must have to add your grid creation code on page_init or page_load.
Note : please make sure your grid creation code not side is postback condition.

Thanks,
Jayesh Goyani
0
Kshitiz
Top achievements
Rank 1
answered on 13 May 2012, 12:03 PM
Sry i dont understand .
User first selects and click the button and den the grid is created  .
Am creating my grid on button click
Like this

public void btn_onclick()
{
if(!postback)--> if i keep this my grid is not created any ideas as to how 
{
grid created here 
}
}
0
Jayesh Goyani
Top achievements
Rank 2
answered on 13 May 2012, 04:48 PM
Hello,

Can you please provide your code?

Thanks,
Jayesh Goyani
0
Kshitiz
Top achievements
Rank 1
answered on 13 May 2012, 05:46 PM

namespace proc.Admin

{

public partial class TotalSeatsMatrix : System.Web.UI.Page

{

RadGrid gridCntrl = new RadGrid();

protected void Page_Load(object sender, EventArgs e)

{

try

{

if (!IsPostBack)

{

}

}

protected void btnclick_OnClick(object sender, EventArgs e)

{

//  if (!IsPostBack)

{

gridCntrl.AutoGenerateColumns = false;

DataSet ds = new DataSet();

BusinessLogic.TotalSeats result = new BusinessLogic.TotalSeats();

BusinessLogic.TotalSeatsEntity objcollection = new BusinessLogic.TotalSeatsEntity();

StringBuilder sb = new StringBuilder();

foreach (RadListBoxItem item in RadListBox1.SelectedItems)

{

sb.Append(item.Value + ",");

}

objcollection.meritBaseId = Convert.ToString(sb.ToString().TrimEnd(','));

objcollection.Course_Id = Convert.ToDecimal(drpCourse.SelectedValue);

ds = result.GetTotalSeats(objcollection);

gridCntrl.EnableViewState = false;

int colCnt = ds.Tables[0].Columns.Count;

int rowCnt = ds.Tables[0].Rows.Count;

GridBoundColumn boundColumn;

boundColumn = new GridBoundColumn();

gridCntrl.MasterTableView.Columns.Add(boundColumn);

gridCntrl.MasterTableView.ShowFooter = true;

boundColumn.DataField = ds.Tables[0].Columns[0].ColumnName;

boundColumn.HeaderText = ds.Tables[0].Columns[0].ColumnName;

gridCntrl.MasterTableView.EnableColumnsViewState = false;

for (int i = 1; i < colCnt; i++)

{

GridTemplateColumn templateColumn = new GridTemplateColumn();

string templateColumnName = ds.Tables[0].Columns[i].ColumnName;

templateColumn.FooterTemplate = new MyTemplate1(templateColumnName);

templateColumn.ItemTemplate = new MyTemplate(templateColumnName);

templateColumn.HeaderText = templateColumnName;

templateColumn.UniqueName = templateColumnName;

templateColumn.DataField = templateColumnName;

gridCntrl.MasterTableView.Columns.Add(templateColumn);

}

GridTemplateColumn templateTotal = new GridTemplateColumn();

string templateColTotalName = "Total";

templateTotal.ItemTemplate = new MyTemplateTotal(templateColTotalName);

templateTotal.FooterTemplate = new MyTemplate1(templateColTotalName);

templateTotal.HeaderText = templateColTotalName;

templateTotal.UniqueName = templateColTotalName;

gridCntrl.MasterTableView.Columns.Add(templateTotal);

gridCntrl.AllowPaging = true;

gridCntrl.PageSize = 20;

gridCntrl.Skin = "Outlook";

PlaceHolder1.Controls.Add(gridCntrl);

gridCntrl.DataSource = ds;

gridCntrl.DataBind();

}

}

private class MyTemplate : ITemplate

{

protected RadNumericTextBox textBox;

private string colname;

//public MyTemplate(string cName,string txtval)

public MyTemplate(string cName)

{

colname = cName;

//textval = txtval;

}

public void InstantiateIn(System.Web.UI.Control container)

{

textBox = new RadNumericTextBox();

textBox.ID = colname;

textBox.DataBinding += new EventHandler(boolValue_DataBinding);

container.Controls.Add(textBox);

}

void boolValue_DataBinding(object sender, EventArgs e)

{

RadNumericTextBox radbox = (RadNumericTextBox)sender;

GridDataItem container = (GridDataItem)radbox.NamingContainer;

radbox.Text = ((DataRowView)container.DataItem)[colname].ToString();

// radbox.Attributes.Add("OnBlur", "test(this);");

//

radbox.Attributes.Add("OnFocus", "Focus(this);");

radbox.Attributes.Add("OnBlur", "Blur(this);");

radbox.Attributes.Add("OnBlur", "BlurRow(this);");

radbox.Attributes.Add("OnFocus", "FocusRow(this);");

radbox.Attributes.Add("ontextchanged", "txtval_TextChanged");

}

}

private class MyTemplate1 : ITemplate

{

protected RadNumericTextBox textBox;

private string colname;

public MyTemplate1(string cName)

{

colname = cName;

}

public void InstantiateIn(System.Web.UI.Control container)

{

textBox = new RadNumericTextBox();

textBox.ID = colname;

textBox.ReadOnly = true;

textBox.Value = 0;

container.Controls.Add(textBox);

}

}

private class MyTemplateTotal : ITemplate

{

protected RadNumericTextBox textBox;

private string colname;

public MyTemplateTotal(string cName)

{

colname = cName;

}

public void InstantiateIn(System.Web.UI.Control container)

{

string str = "ctl";

int countrow = 11;

str = "ctl" + countrow.ToString();

try

{

textBox = new RadNumericTextBox();

textBox.ID = colname;

textBox.ReadOnly = true;

textBox.Value = 0;

container.Controls.Add(textBox);

countrow++;

str = "ctl" + countrow.ToString();

}

catch (Exception)

{

throw;

}

}

}

protected void lnkUpdateAll_Click(object sender, EventArgs e)

{

try

{

for (int OuterCol = 1; OuterCol < gridCntrl.Columns.Count; OuterCol++)

{

}

}

catch (Exception)

{

throw;

}

}


I want GRID access over here  But not getting anyitems from NULL EXCEPTION ERROR


private void UpdateData()

{

for (int OuterCol = 1; OuterCol < gridCntrl.Columns.Count; OuterCol++)

{

for (int inner = 0; inner < gridCntrl.Items.Count; inner++)

{

double total;

string id;

if (true)

{

}

else if (gridCntrl.Items[OuterCol].ItemType == GridItemType.Footer)

{

}

}

}

}

}

}




0
Jayesh Goyani
Top achievements
Rank 2
answered on 14 May 2012, 01:58 PM
Hello,

you are adding grid creation code on button Click.

You Must have to add your page creation code in  Page_load OR page_Init.

protected void Page_Load(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(Request.Params["__EVENTTARGET"]))
        {
            if (Request.Params["__EVENTTARGET"].ToString() == "btnclick" || Request.Params["__EVENTTARGET"].ToString() == "lnkUpdateAll")
            {
                // Grid Creation Code
            }
        }
    }

After doing above step you are able to get columns in radgrid on button click event.

Thanks,
Jayesh Goyani
0
Kshitiz
Top achievements
Rank 1
answered on 14 May 2012, 03:03 PM
i have tried that code you gave , still it never ever goes inside it !
checked on debug also !
0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 15 May 2012, 06:41 AM
Hello,

Please check below code snippet or Demo.
Its work correctly.
<head runat="server">
    <title></title>
    
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
        </telerik:RadScriptManager>
        
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="btnclick_OnClick"
            UseSubmitBehavior="false" />
        <asp:Button ID="Button2" runat="server" Text="Button" OnClick="lnkUpdateAll_Click"
            UseSubmitBehavior="false" />
    </div>
    </form>
</body>
</html>
public partial class forumpage : System.Web.UI.Page
{
 
 
    RadGrid gridCntrl = new RadGrid();
 
    protected void Page_Init(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(Request.Params["__EVENTTARGET"]))
        {
            if (Request.Params["__EVENTTARGET"].ToString() == "Button1" || Request.Params["__EVENTTARGET"].ToString() == "Button2")
            {
                creategrid();
            }
        }
         
         
    }
 
    protected void Page_Load(object sender, EventArgs e)
    {
         
 
 
    }
 
 
    protected void btnclick_OnClick(object sender, EventArgs e)
    {
 
       
 
    }
 
    protected void lnkUpdateAll_Click(object sender, EventArgs e)
    {
 
        //try
        //{
 
            for (int OuterCol = 1; OuterCol < gridCntrl.Columns.Count; OuterCol++)
            {
 
            }
 
        //}
 
        //catch (Exception)
        //{
 
        //    throw;
 
        //}
 
    }
 
 
    public void creategrid()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("ID", typeof(int));
        dt.Columns.Add("Name", typeof(string));
        dt.Rows.Add(1, "name1");
        dt.Rows.Add(2, "name2");
 
 
        gridCntrl.AutoGenerateColumns = false;
 
        GridBoundColumn boundColumn;
 
        boundColumn = new GridBoundColumn();
 
        gridCntrl.MasterTableView.Columns.Add(boundColumn);
 
        gridCntrl.MasterTableView.ShowFooter = true;
 
        boundColumn.DataField = dt.Columns[0].ToString();
 
        boundColumn.HeaderText = dt.Columns[0].ToString();
 
        gridCntrl.MasterTableView.EnableColumnsViewState = false;
 
        for (int i = 1; i < dt.Columns.Count; i++)
        {
 
            GridTemplateColumn templateColumn = new GridTemplateColumn();
 
            string templateColumnName = dt.Columns[i].ToString();
 
            templateColumn.FooterTemplate = new MyTemplate1(templateColumnName);
 
            templateColumn.ItemTemplate = new MyTemplate(templateColumnName);
 
            templateColumn.HeaderText = templateColumnName;
 
            templateColumn.UniqueName = templateColumnName;
 
            templateColumn.DataField = templateColumnName;
 
            gridCntrl.MasterTableView.Columns.Add(templateColumn);
 
        }
 
        GridTemplateColumn templateTotal = new GridTemplateColumn();
 
        string templateColTotalName = "Total";
 
        templateTotal.ItemTemplate = new MyTemplateTotal(templateColTotalName);
 
        templateTotal.FooterTemplate = new MyTemplate1(templateColTotalName);
 
        templateTotal.HeaderText = templateColTotalName;
 
        templateTotal.UniqueName = templateColTotalName;
 
        gridCntrl.MasterTableView.Columns.Add(templateTotal);
 
        gridCntrl.AllowPaging = true;
 
        gridCntrl.PageSize = 20;
 
        gridCntrl.Skin = "Outlook";
 
        this.form1.Controls.Add(gridCntrl);
 
        gridCntrl.DataSource = dt;
 
        gridCntrl.DataBind();
    }
 
    private class MyTemplate : ITemplate
    {
 
        protected RadNumericTextBox textBox;
 
        private string colname;
 
        //public MyTemplate(string cName,string txtval)
 
        public MyTemplate(string cName)
        {
 
            colname = cName;
 
            //textval = txtval;
 
        }
 
        public void InstantiateIn(System.Web.UI.Control container)
        {
 
            textBox = new RadNumericTextBox();
 
            textBox.ID = colname;
 
            textBox.DataBinding += new EventHandler(boolValue_DataBinding);
 
            container.Controls.Add(textBox);
 
        }
 
        void boolValue_DataBinding(object sender, EventArgs e)
        {
 
            RadNumericTextBox radbox = (RadNumericTextBox)sender;
 
            GridDataItem container = (GridDataItem)radbox.NamingContainer;
 
            radbox.Text = "0";
 
            // radbox.Attributes.Add("OnBlur", "test(this);");
 
            //
 
            radbox.Attributes.Add("OnFocus", "Focus(this);");
 
            radbox.Attributes.Add("OnBlur", "Blur(this);");
 
            radbox.Attributes.Add("OnBlur", "BlurRow(this);");
 
            radbox.Attributes.Add("OnFocus", "FocusRow(this);");
 
            radbox.Attributes.Add("ontextchanged", "txtval_TextChanged");
 
        }
 
    }
 
    private class MyTemplate1 : ITemplate
    {
 
        protected RadNumericTextBox textBox;
 
        private string colname;
 
        public MyTemplate1(string cName)
        {
 
            colname = cName;
 
        }
 
        public void InstantiateIn(System.Web.UI.Control container)
        {
 
            textBox = new RadNumericTextBox();
 
            textBox.ID = colname;
 
            textBox.ReadOnly = true;
 
            textBox.Value = 0;
 
            container.Controls.Add(textBox);
 
        }
 
    }
 
    private class MyTemplateTotal : ITemplate
    {
 
        protected RadNumericTextBox textBox;
 
        private string colname;
 
        public MyTemplateTotal(string cName)
        {
 
            colname = cName;
 
        }
 
        public void InstantiateIn(System.Web.UI.Control container)
        {
 
            string str = "ctl";
 
            int countrow = 11;
 
            str = "ctl" + countrow.ToString();
 
            try
            {
 
                textBox = new RadNumericTextBox();
 
                textBox.ID = colname;
 
                textBox.ReadOnly = true;
 
                textBox.Value = 0;
 
                container.Controls.Add(textBox);
 
                countrow++;
 
                str = "ctl" + countrow.ToString();
 
            }
 
            catch (Exception)
            {
 
                throw;
 
            }
 
        }
 
    }
 
     
 
 
 
 
 
    private void UpdateData()
    {
 
        for (int OuterCol = 1; OuterCol < gridCntrl.Columns.Count; OuterCol++)
        {
 
            for (int inner = 0; inner < gridCntrl.Items.Count; inner++)
            {
 
                double total;
 
                string id;
 
                if (true)
                {
 
                }
 
                else if (gridCntrl.Items[OuterCol].ItemType == GridItemType.Footer)
                {
 
                }
 
            }
 
        }
 
    }
 
 
}


Thanks,
Jayesh Goyani
0
Kshitiz
Top achievements
Rank 1
answered on 16 May 2012, 08:33 PM
hey jayesh bhai it worked properly thanxxx aloot...
can u tell me wat  actually did u do?
i cant understand during update .. it calls the grid function and also gets the predefined ds with old values , but during display i can see the new values which i typed in radtext box ?? how come ?

Ur indeed MVP ! :) 
LET ME know !
0
Jayesh Goyani
Top achievements
Rank 2
answered on 17 May 2012, 09:48 AM
Hello,

When we are add any control dynamically then we must have to add it on every time in Page_load or page_init.
in page initilaize event view state is generated for that control.

In your previously code
1. you are added grid in button1's click event - so it is not create proper viewstate
2. when you click on button2 you are not added grid again - so there is not any grid exists in your viewstate/code


Thanks,
Jayesh Goyani
Tags
Grid
Asked by
Kshitiz
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Kshitiz
Top achievements
Rank 1
Share this question
or