Vinz' Blog

"Code, Beer and Music" ~ my way of being a programmer!
posts - 124, comments - 366, trackbacks - 0

My Links

News

Archives

Image Galleries

Wednesday, August 12, 2009

Retain Leading Zeros when Converting from Int to String

Many members in the forums are asking if why is it that an integer value with a leading zero is being trimmed off when converting it to a string?

 

Consider this example:

 

        int num = 0123456; // a 7 numbers

        string sNum = num.ToString();

 

        //The result will give you 123456

 

 

What happened to the leading zero?

 

Well basically, leading zero has no significance to an integer because by nature an integer with a value of 01 is simply the same as 1. So based on the example above we have an integer value of 0123456 and basically it outputs 123456 (without the leading zero).

 

How can we retain them when we need to display them?

 

That’s the main reason why I decided to write this post. Here’s one way on how to retain them when converting it to string.

 

        int num = 0123456; // a 7 numbers

        string sNum = num.ToString("0000000");

 

        Response.Write(sNum);

        //The result will give you 0123456

 

The trick there is we used ToString() with the format "0000000" where the number of zero is equal to number of digits your integer has. So since we have a 7 digit integer value then we used 7 zeros in the format.

 

That’s it! Hope you will find this post useful!

posted @ Wednesday, August 12, 2009 8:56 PM | Feedback (0) |

UPDATED: Adding Dynamic Rows in ASP.NET GridView Control with TextBoxes and with Delete functionality

In my previous post, I have posted the updated the codes about Adding Dynamic Rows in ASP.Net GridView Control with TextBoxes because there is a bug on that. So obviously the codes in my other example about Adding a Delete functionality in Dynamic TextBoxes in GridView is affected. To fix the issue then you can refer to this updated code below:

 

    protected void LinkButton1_Click(object sender, EventArgs e)

    {

        LinkButton lb = (LinkButton)sender;

        GridViewRow gvRow = (GridViewRow) lb.NamingContainer;

        int rowID = gvRow.RowIndex;

        if (ViewState["CurrentTable"] != null)

        {

          DataTable  dt = (DataTable)ViewState["CurrentTable"];

          if (dt.Rows.Count > 1)

          {

              if (gvRow.RowIndex < dt.Rows.Count -1)

              {

                  //Remove the Selected Row data

                  dt.Rows.Remove(dt.Rows[rowID]);

                  ResetRowID(dt);

              }

          }

            //Store the current data in ViewState for future reference

            ViewState["CurrentTable"] = dt;

            //Re bind the GridView for the updated data

            Gridview1.DataSource = dt;

            Gridview1.DataBind();

        }

 

        //Set Previous Data on Postbacks

        SetPreviousData();

    }

 

    private void ResetRowID(DataTable dt)

    {

        int rowNumber = 1;

        if (dt.Rows.Count > 0)

        {

            foreach (DataRow row in dt.Rows)

            {

                row[0] = rowNumber;

                rowNumber++;

            }

        }

    }

 

If you notice, I have added the method ResetRowID() so that the row number will be sequenced when deleting a row in the Grid.

 

That’s it! Happy coding!

posted @ Wednesday, August 12, 2009 5:21 PM | Feedback (2) |

UPDATED: Adding Dynamic Rows in ASP.Net GridView Control with TextBoxes

Well its seems that there is a little bug with my previous article about “ Adding Dynamic Rows in ASP.Net GridView Control with TextBoxes “. The problem is that whenever you change the value of the previous data in the TextBox the updated values will not reflect on postbacks. So I have modified a bit of my codes at AddNewRowToGrid() and SetPreviousData() methods to fix the issue.

 

Here are the code blocks below for the updates:

 

    private void AddNewRowToGrid()

    {

        int rowIndex = 0;

 

        if (ViewState["CurrentTable"] != null)

        {

            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];

            DataRow drCurrentRow = null;

            if (dtCurrentTable.Rows.Count > 0)

            {

                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)

                {

                    //extract the TextBox values

                    TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");

                    TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");

                    TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");

 

                    drCurrentRow = dtCurrentTable.NewRow();

                    drCurrentRow["RowNumber"] = i + 1;

 

                    dtCurrentTable.Rows[i - 1]["Column1"] = box1.Text;

                    dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text;

                    dtCurrentTable.Rows[i - 1]["Column3"] = box3.Text;

 

                    rowIndex++;

                }

                dtCurrentTable.Rows.Add(drCurrentRow);

                ViewState["CurrentTable"] = dtCurrentTable;

 

                Gridview1.DataSource = dtCurrentTable;

                Gridview1.DataBind();

            }

        }

        else

        {

            Response.Write("ViewState is null");

        }

 

        //Set Previous Data on Postbacks

        SetPreviousData();

    }

 

 

    private void SetPreviousData()

    {

        int rowIndex = 0;

        if (ViewState["CurrentTable"] != null)

        {

            DataTable dt = (DataTable)ViewState["CurrentTable"];

            if (dt.Rows.Count > 0)

            {

                for (int i = 0; i < dt.Rows.Count; i++)

                {

                    TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");

                    TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");

                    TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");

 

                    box1.Text = dt.Rows[i]["Column1"].ToString();

                    box2.Text = dt.Rows[i]["Column2"].ToString();

                    box3.Text = dt.Rows[i]["Column3"].ToString();

 

                    rowIndex++;

                }

            }

        }

    }

 

That’s it! Let me know if you find any bugs.

posted @ Wednesday, August 12, 2009 5:08 PM | Feedback (0) |

Powered by: