How to get dynamic column rows value of gridview inside forloop

mukesh1
 
on May 12, 2022 05:41 AM
222 Views

 I have 2 gridview grdvieMarks for add onlin questionAnssheet and GridviewMaxmarks for adding maxmarks for each question. I know only first 2 column of gridviewmarks , id and name of each row, and other columns count dynamic and these 2 column are also dynamic. on save button i want to find each id, admission number and max marks of that question

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:GridView ID="grdvieMarks" runat="server"></asp:GridView>
        <div id="dimxmatks" style="overflow: auto">
            <asp:GridView ID="GridviewMaxmarks" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridviewMaxmarks_RowDataBound"></asp:GridView>
        </div>
        <asp:Button ID="btnsave" runat="server" Text="Save" OnClick="btnsave_Click" />
    </ContentTemplate>

    <Triggers>
        <asp:PostBackTrigger ControlID="btnsave" />
    </Triggers>
</asp:UpdatePanel>

 

rdvieMarks.DataSource = dsCustomers;
grdvieMarks.DataBind();
{
    GridviewMaxmarks.Columns.Clear();
    DataTable dt = new DataTable();
    int cols = Convert.ToInt32(dsCustomers.Tables[0].Columns.Count - 2);
    int rows = Convert.ToInt32(dsCustomers.Tables[0].Rows.Count);
    for (int i = 0; i < cols; i++)
    {
        TemplateField field = new TemplateField();
        field.HeaderText = dsCustomers.Tables[0].Columns[i + 2].ColumnName.ToString();

        GridviewMaxmarks.Columns.Add(field);
    }

    if (rows > 0)
    {
        dt.Rows.Add();
    }

    GridviewMaxmarks.DataSource = dt;
    GridviewMaxmarks.DataBind();

 

DataTable dt2 = new DataTable();
{
    for (int i = 0; i < GridviewMaxmarks.Columns.Count; i++)
    {
        dt.Columns.Add("column" + i.ToString());
    }
    foreach (GridViewRow row in GridviewMaxmarks.Rows)
    {
        DataRow dr2 = dt.NewRow();
        for (int j = 0; j < GridviewMaxmarks.Columns.Count; j++)
        {
            dr2["column" + j.ToString()] = row.Cells[j].Text;
        }
        dt.Rows.Add(dr2);
    }
}

 

protected void btnsave_Click(object sender, EventArgs e)
{
    for (int i = 0; i < grdvieMarks.Rows.Count; i++)
    {
        clsQuestionAnsSheet obj = new clsQuestionAnsSheet();

        obj.AdmissionNo = grdvieMarks.Rows[i][0].Text;
        for (int j = 2; j < grdvieMarks.Columns.Count; j++)
        {
            string columnname = grdvieMarks.Columns[j].ToString();
            obj.Subject_id = "1";
            obj.Session = Session["2021-2022"].ToString();
            obj.UserId = Session["id"].ToString();
            obj.QuestionNo = columnname;
        }
    }
}

protected void GridviewMaxmarks_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        for (int i = 0; i < GridviewMaxmarks.Columns.Count; i++)
        {
            int rowID = e.Row.RowIndex;
            TextBox textbox = new TextBox();
            //textbox.CssClass = "form-control input-sm";
            textbox.ID = GridviewMaxmarks.Columns[i].ToString();
            RegularExpressionValidator regex = new RegularExpressionValidator();
            regex.ID = "Regex" + GridviewMaxmarks.Columns[i].ToString();
            regex.ControlToValidate = textbox.ID;
            regex.Display = ValidatorDisplay.Dynamic;
            regex.SetFocusOnError = true;
            regex.ValidationExpression = "^([0-9]\\d*)(\\.\\d{1,2})?$";
            regex.ErrorMessage = "INvalid Value";

            e.Row.Cells[i].Controls.Add(textbox);
            e.Row.Cells[i].Controls.Add(regex);
        }
    }
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
Results 1 - 5 of 6
dharmendr
 
on May 12, 2022 05:41 AM
mukesh1 says:
on save button i want to find each id, admission number and max marks of that question

 Please explain with example.

mukesh1
 
on May 12, 2022 05:50 AM
on May 12, 2022 06:05 AM

I get data in dataset , only ist 2 column(id and emplyeename) name are same , otherwise all dyanmic(column name questionno, and value is its answer).

also want to apply regex on maxmarks gridview.

 

i firstly bind it in gridviewmarks, then i create another gridview to enter maxmarks of each question.

on save i want to find 

id ,questionno,ans(value of each question),maxmarks enterd 

 

https://pasteboard.co/F7DBMpjxP4Dr.jpg

arjunv
 
on May 12, 2022 08:40 AM

I will get back to you tomoorow.

dharmendr
 
on May 13, 2022 05:42 AM

You need to loop through each row. Then inside the row loop through the columns and calculate the max mark.

Refer below example.

HTML

<asp:GridView ID="GridviewMaxmarks" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField HeaderText="Id" DataField="Id" />
        <asp:BoundField HeaderText="Candidate_Name" DataField="Candidate_Name" />
        <asp:TemplateField HeaderText="Q1">
            <ItemTemplate>
                <asp:TextBox ID="txtQ1" Text='<%# Eval("Q1") %>' Width="40px" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Q2">
            <ItemTemplate>
                <asp:TextBox ID="txtQ2" Text='<%# Eval("Q2") %>' Width="40px" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Q3">
            <ItemTemplate>
                <asp:TextBox ID="txtQ3" Text='<%# Eval("Q3") %>' Width="40px" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Code

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt2 = new DataTable();
        dt2.Columns.AddRange(new DataColumn[] {
                                new DataColumn("Id", typeof(int)),
                                new DataColumn("Candidate_Name"),
                                new DataColumn("Q1", typeof(int)),
                                new DataColumn("Q2", typeof(int)),
                                new DataColumn("Q3", typeof(int))
        });
        dt2.Rows.Add(1, "Aarush Sharma", 1, 2, 3);
        dt2.Rows.Add(2, "Akshit Rana", 6, 4, 5);
        dt2.Rows.Add(3, "Anhad Singh", 5, 9, 4);
        this.GridviewMaxmarks.DataSource = dt2;
        this.GridviewMaxmarks.DataBind();
    }
}

protected void btnsave_Click(object sender, EventArgs e)
{
    List<MarkDetails> details = new List<MarkDetails>();
    foreach (GridViewRow row in GridviewMaxmarks.Rows)
    {
        MarkDetails detail = new MarkDetails();
        detail.Id = Convert.ToInt32(row.Cells[0].Text);
        detail.Name = row.Cells[1].Text;
        List<int> marks = new List<int>();
        for (int i = 2; i < row.Cells.Count; i++)
        {
            marks.Add(Convert.ToInt32((row.Cells[i].Controls[1] as TextBox).Text));
        }
        detail.Mark = marks.Max();
        details.Add(detail);
    }
}

public class MarkDetails
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Mark { get; set; }
}

 

mukesh1
 
on May 13, 2022 05:50 AM

sir column in datset is dynamic not fixed numbers.

Results 1 - 5 of 6