Gridview random selection

Posted by Gayathri under ASP.NET on 9/3/2012 | Points: 10 | Views : 1629 | Status : [Member] | Replies : 2
I want to select any 2 rows in random from a grid .
for example i have a textbox followed by a button.

if i enter 2 in the text box and click the button , it should highlight the first 2 or middle two or last two.




Responses

Posted by: Sureshknkt7 on: 9/3/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

This is code for selecting user specific rows with range and value in Gridview.

Ex:

If u r giving input as 2 then it selected two rows i.e, 2row and 3rd rows.

if u r giving input as 4 in textbox then it selected four rows in GridView like 4,5,6,7 rows.


Code:

In Aspx page(Design page):

<asp:TextBox ID="txtRange" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btnSubmit" runat="server" Text="GetSelect" OnClick="Get_Select" />
<br />
<asp:Button ID="Button1" runat="server" Text="GetData" />

<asp:GridView ID="gridview1" runat="server" DataKeyNames="AutoId">
<SelectedRowStyle BackColor="Yellow" />
</asp:GridView>


In Aspx.cs page:

In Select Button Onclick Event write the following code:

protected void Get_Select(object sender, EventArgs e)
{
int range = Convert.ToInt32(txtRange.Text);
foreach (GridViewRow row in gridview1.Rows)
{



int autoID = Convert.ToInt32(gridview1.DataKeys[row.RowIndex].Value);
if ((autoID >= range && range > 0) || (autoID == range))
{
row.BackColor = System.Drawing.Color.Red;
range--;
autoID++;

}

if (range == 0)
break;


}

}


Thank you,
Suresh K










Gayathri, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vasanthmvp on: 9/3/2012 [Member] Starter | Points: 25

Up
0
Down
Hi, here is the simple solution for you.

starting with the entered value, it should go on highlight till the same number entered. so just highlight the rows till the entered value gets twice of it.

aspx page:

<asp:TextBox ID="t1" runat="server" />
<asp:Button ID="btn" runat="server" OnClick="highlight" Text="Highlight" />
<asp:GridView ID="PopulateDataGridView" runat="server" />


.aspx.cs

// Here im getting my gridview.

string _connstr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
int y = 0;
int z;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetData();
}
}

protected DataTable GetData()
{
using (SqlConnection conn = new SqlConnection(_connstr))
{
string sql = "Select AutoId, FirstName, LastName from PersonalDetails";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
{
DataTable table = new DataTable();
ad.Fill(table);
PopulateDataGridView.DataSource = table;
PopulateDataGridView.DataBind();
return table;
}
}
}

button method:

1. If a user enters 2, then to effect the 2nd & 3rd rows of gridview. (This can be in starting/middle/end based on the user input.)

protected void highlight(object sender, EventArgs e)
{
try
{
int x = int.Parse(t1.Text);
z = x;
while (true)
{
PopulateDataGridView.Rows[x - 1].Attributes.Add("style", "background-color:green");
x++;
if (x == 2 * z)
{
break;
}
}
}
catch (Exception ee)
{
Response.Write("Entered Number is out of the Range.");
}
}


2. If you want to highlight first 2 rows (suppose user entered 2 in the textbox), then change button method.

Proctected void highlight(Object sender, EventArgs e)
{

int x = int.Parse(t1.Text);
for(int i = 0; i<x; i++)
{
PopulateDataGridView.Rows[i].Attributes.Add("style", "background-color:green");
}
}


Regards,


Awesome Coding !! :)

Gayathri, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response